總有時候會有些需求, 需要用到秒, 比如 JedisCluster 設置過期時間
現在有一個需求是 : 查詢接口的緩存設置有效期為:1天+隨機時間
基本可以按以下來做:
package com.lwc.demo;
import java.util.Random;
/**
* @author eddie.lee
* @Package com.lwc.demo
* @ClassName RandomTest
* @description 一天的秒數, 變換一天的秒的隨機數,轉換為一天的時分秒
* @date created in 2018-04-19 11:00
* @modified by
*/
public class RandomUtils {
/**
* random min vaule
*/
private static int MIX = 1;
/**
* 86400s = 1d
*/
private static int MAX = 86400;
/**
* 一天內的隨機秒數
*/
private static void randomMain() {
Random random = new Random();
int randomDay = random.nextInt(MAX) % (MAX - MIX + 1) + MIX;
System.out.println("一天秒的隨機數: " + randomDay);
int seconds = randomDay;
int d = seconds / (60 * 60 * 24);
int h = (seconds - (60 * 60 * 24 * d)) / 3600;
int m = (seconds - 60 * 60 * 24 * d - 3600 * h) / 60;
int s = seconds - 60 * 60 * 24 * d - 3600 * h - 60 * m;
System.out.println(seconds + " = " + d + "天" + h + "時" + m + "分" + s + "秒");
}
public static void main(String[] args) {
RandomTest.randomMain();
}
}
Print:
一天秒的隨機數: 30317
30317 = 0天8時25分17秒
