zoukankan      html  css  js  c++  java
  • 【Java】 秒转时分秒天

    总有时候会有些需求, 需要用到秒, 比如 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秒

  • 相关阅读:
    前端CSS部分简单整理
    前端HTML部分简单整理
    Top Android App使用的组件
    使用DialogFragment创建对话框总结
    Rails常用命令
    developer.android.com笔记
    Google Maps API v2 Demo Tutorial
    Android学习的一些问题
    Android学习过程
    Beginning Android 4 Programming Book学习
  • 原文地址:https://www.cnblogs.com/EddieBlog/p/8882155.html
Copyright © 2011-2022 走看看