日期工具类TimeUnit

简介

TimeUnit是Java.util.concurrent包下面的一个类。它提供了两大功能:

1)提供了可读性更好的线程暂停操作,通常用来替换Thread.sleep();
2)提供了便捷方法用于把时间转换成不同单位,如把秒转换成毫秒;

代码示例

package com.shen.test.other;

import java.sql.Date;
import java.text.DateFormat;
import java.util.concurrent.TimeUnit;

/** * * @Description 时间工具类测试 <BR> * * @author shenhaiwen * * @since 2017年4月13日上午9:34:05 */
public class TimeUnitTest {

    public static void main(String[] args) throws InterruptedException {
        // 1、时间转换功能
        TimeUnit timeUnit = TimeUnit.DAYS; //单位为天
        printTime(timeUnit,1);
        timeUnit = TimeUnit.HOURS; //单位为小时
        printTime(timeUnit,1);
        timeUnit = TimeUnit.MINUTES; //单位为分钟
        printTime(timeUnit,1);
        timeUnit = TimeUnit.SECONDS; //单位为秒
        printTime(timeUnit,1);
        // 2、线程暂停操作功能
        System.out.println("Start Sleeping for 1 minutes using Thread.sleep(),Time:"+System.currentTimeMillis());
        Thread.sleep(1 * 60 * 1000);
        System.out.println("Sleeping for 1 second using TimeUnit sleep()"+System.currentTimeMillis());
        TimeUnit.SECONDS.sleep(1);
        System.out.println("Sleeping for 1 minute1 using TimeUnit sleep()"+System.currentTimeMillis());
        TimeUnit.MINUTES.sleep(1);
        System.out.println("End Sleeping for 1 minute1 using TimeUnit sleep()"+System.currentTimeMillis());
         //TimeUnit.HOURS.sleep(1);
        // TimeUnit.DAYS.sleep(1);

    }

    public static void printTime(TimeUnit timeUnit,int time){
        System.out.println("======= begin =======,input:"+time+timeUnit.name());
        System.out.println("timeUnit.toNanos:"+timeUnit.toNanos(time));
        System.out.println("timeUnit.toMicros:"+timeUnit.toMicros(time));
        System.out.println("timeUnit.toDays:"+timeUnit.toDays(time));
        System.out.println("timeUnit.toHours:"+timeUnit.toHours(time));
        System.out.println("timeUnit.toMinutes:"+timeUnit.toMinutes(time));
        System.out.println("timeUnit.toSeconds:"+timeUnit.toSeconds(time));
        System.out.println("timeUnit.toMillis:"+timeUnit.toMillis(time));
        System.out.println("1天是"+(timeUnit.convert(1, TimeUnit.DAYS))+timeUnit.name());
        System.out.println("12小时是"+(timeUnit.convert(12, TimeUnit.HOURS))+timeUnit.name());
        System.out.println("3600秒是"+(timeUnit.convert(36000, TimeUnit.MINUTES))+timeUnit.name());
        System.out.println("======= end =======");


    }

}

结果输出:

======= begin =======,input:1DAYS
timeUnit.toNanos:86400000000000
timeUnit.toMicros:86400000000
timeUnit.toDays:1
timeUnit.toHours:24
timeUnit.toMinutes:1440
timeUnit.toSeconds:86400
timeUnit.toMillis:86400000
1天是1DAYS
12小时是0DAYS
3600秒是25DAYS
======= end =======
======= begin =======,input:1HOURS
timeUnit.toNanos:3600000000000
timeUnit.toMicros:3600000000
timeUnit.toDays:0
timeUnit.toHours:1
timeUnit.toMinutes:60
timeUnit.toSeconds:3600
timeUnit.toMillis:3600000
1天是24HOURS
12小时是12HOURS
3600秒是600HOURS
======= end =======
======= begin =======,input:1MINUTES
timeUnit.toNanos:60000000000
timeUnit.toMicros:60000000
timeUnit.toDays:0
timeUnit.toHours:0
timeUnit.toMinutes:1
timeUnit.toSeconds:60
timeUnit.toMillis:60000
1天是1440MINUTES
12小时是720MINUTES
3600秒是36000MINUTES
======= end =======
======= begin =======,input:1SECONDS
timeUnit.toNanos:1000000000
timeUnit.toMicros:1000000
timeUnit.toDays:0
timeUnit.toHours:0
timeUnit.toMinutes:0
timeUnit.toSeconds:1
timeUnit.toMillis:1000
1天是86400SECONDS
12小时是43200SECONDS
3600秒是2160000SECONDS
======= end =======
Start Sleeping for 1 minutes using Thread.sleep(),Time:1492053379501
Sleeping for 1 second using TimeUnit sleep()1492053439501
Sleeping for 1 minute1 using TimeUnit sleep()1492053440501
End Sleeping for 1 minute1 using TimeUnit sleep()1492053500501

你可能感兴趣的:(线程,日期,timeunit,日期工具类)