TimeUnit的用法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TimeUnit的用法相关的知识,希望对你有一定的参考价值。


一、概述


TimeUnit是java.util.concurrent包下面的一个类,表示给定单元粒度的时间段
作用:
1、时间颗粒度转换
2、延时

TimeUnit类介绍


public enum TimeUnit 
NANOSECONDS //纳秒
public long toNanos(long d) return d;
public long toMicros(long d) return d/(C1/C0);
public long toMillis(long d) return d/(C2/C0);
public long toSeconds(long d) return d/(C3/C0);
public long toMinutes(long d) return d/(C4/C0);
public long toHours(long d) return d/(C5/C0);
public long toDays(long d) return d/(C6/C0);
public long convert(long d, TimeUnit u) return u.toNanos(d);
int excessNanos(long d, long m) return (int)(d - (m*C2));
,
MICROSECONDS // 微秒
public long toNanos(long d) return x(d, C1/C0, MAX/(C1/C0));
public long toMicros(long d) return d;
public long toMillis(long d) return d/(C2/C1);
public long toSeconds(long d) return d/(C3/C1);
public long toMinutes(long d) return d/(C4/C1);
public long toHours(long d) return d/(C5/C1);
public long toDays(long d) return d/(C6/C1);
public long convert(long d, TimeUnit u) return u.toMicros(d);
int excessNanos(long d, long m) return (int)((d*C1) - (m*C2));
,
MILLISECONDS //毫秒
public long toNanos(long d) return x(d, C2/C0, MAX/(C2/C0));
public long toMicros(long d) return x(d, C2/C1, MAX/(C2/C1));
public long toMillis(long d) return d;
public long toSeconds(long d) return d/(C3/C2);
public long toMinutes(long d) return d/(C4/C2);
public long toHours(long d) return d/(C5/C2);
public long toDays(long d) return d/(C6/C2);
public long convert(long d, TimeUnit u) return u.toMillis(d);
int excessNanos(long d, long m) return 0;
,
SECONDS //秒
public long toNanos(long d) return x(d, C3/C0, MAX/(C3/C0));
public long toMicros(long d) return x(d, C3/C1, MAX/(C3/C1));
public long toMillis(long d) return x(d, C3/C2, MAX/(C3/C2));
public long toSeconds(long d) return d;
public long toMinutes(long d) return d/(C4/C3);
public long toHours(long d) return d/(C5/C3);
public long toDays(long d) return d/(C6/C3);
public long convert(long d, TimeUnit u) return u.toSeconds(d);
int excessNanos(long d, long m) return 0;
,
MINUTES //分
public long toNanos(long d) return x(d, C4/C0, MAX/(C4/C0));
public long toMicros(long d) return x(d, C4/C1, MAX/(C4/C1));
public long toMillis(long d) return x(d, C4/C2, MAX/(C4/C2));
public long toSeconds(long d) return x(d, C4/C3, MAX/(C4/C3));
public long toMinutes(long d) return d;
public long toHours(long d) return d/(C5/C4);
public long toDays(long d) return d/(C6/C4);
public long convert(long d, TimeUnit u) return u.toMinutes(d);
int excessNanos(long d, long m) return 0;
,
HOURS //时
public long toNanos(long d) return x(d, C5/C0, MAX/(C5/C0));
public long toMicros(long d) return x(d, C5/C1, MAX/(C5/C1));
public long toMillis(long d) return x(d, C5/C2, MAX/(C5/C2));
public long toSeconds(long d) return x(d, C5/C3, MAX/(C5/C3));
public long toMinutes(long d) return x(d, C5/C4, MAX/(C5/C4));
public long toHours(long d) return d;
public long toDays(long d) return d/(C6/C5);
public long convert(long d, TimeUnit u) return u.toHours(d);
int excessNanos(long d, long m) return 0;
,
DAYS //天
public long toNanos(long d) return x(d, C6/C0, MAX/(C6/C0));
public long toMicros(long d) return x(d, C6/C1, MAX/(C6/C1));
public long toMillis(long d) return x(d, C6/C2, MAX/(C6/C2));
public long toSeconds(long d) return x(d, C6/C3, MAX/(C6/C3));
public long toMinutes(long d) return x(d, C6/C4, MAX/(C6/C4));
public long toHours(long d) return x(d, C6/C5, MAX/(C6/C5));
public long toDays(long d) return d;
public long convert(long d, TimeUnit u) return u.toDays(d);
int excessNanos(long d, long m) return 0;
;

三、用法



延迟:



@Test
public void testTime()
new Thread( new Runnable()

@Override
public void run()
try
TimeUnit.SECONDS.sleep(10);
System.out.println("延时10秒,完成了");
catch (InterruptedException e)
e.printStackTrace();


).start();


结合redis在项目中的使用



String key = RedisKeyUtils.getUserTokenKey(userId);
BoundValueOperations<String, String> options = redisTemplate.boundValueOps(key);
options.set(token);
options.expire(15, TimeUnit.DAYS);



以上是关于TimeUnit的用法的主要内容,如果未能解决你的问题,请参考以下文章

ThreadPoolExcutor用法详解

Java + selenium Timeout接口用法介绍

TimeUnit 笔记

TimeUnit枚举类

如何mock timeunit.seconds.sleep

JUC——TimeUnit工具类