- 使用 Spring Initializr 创建 Spring Boot 应用程序
- 在Spring Boot中配置Cassandra
- 在 Spring Boot 上配置 Tomcat 连接池
- 将Camel消息路由到嵌入WildFly的Artemis上
JavaAPI:https://docs.oracle.com/javase/8/docs/api/index.html
api:https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/TimeUnit.html
TimeUnit是java.util.concurrent包下面的一个类;TimeUnit 是Java枚举应用场景中最好的例子之一,所有TimeUnit都是枚举实例;
TimeUnit提供了更加优雅的线程sleep写法;TimeUnit还提供了各种时间单位转换的方法;
NANOSECONDS //毫微秒 十亿分之一秒【1微秒/1000】
MICROSECONDS //微秒 一百万分之一秒【1毫秒/1000】
MILLISECONDS //毫秒 千分之一秒
SECONDS //秒
MINUTES //分钟
HOURS //小时
DAYS //天
//NANOSECONDS MICROSECONDS MILLISECONDS SECONDS MINUTES HOURS DAYS
Arrays.stream(TimeUnit.values())
.forEach((item -> System.out.print(item + " ")));
Modifier and Type | Method and Description |
---|---|
static void |
sleep(long millis) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. |
static void |
sleep(long millis, int nanos) Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds plus the specified number of nanoseconds, subject to the precision and accuracy of system timers and schedulers. |
TimeUnit提供了更加优雅的线程睡眠写法
//休眠40分种的三种写法
//
Thread.sleep(2400000);
//
Thread.sleep(40 * 60 * 1000);
// 可读性增强了
TimeUnit.MINUTES.sleep(40);
//sleep 40分组
Thread.sleep(2400000);
Thread.sleep(TimeUnit.MINUTES.toMillis(40));
Modifier and Type | Method and Description |
---|---|
long |
convert(long sourceDuration, TimeUnit sourceUnit) Converts the given time duration in the given unit to this unit. |
//天转化成小时
TimeUnit.HOURS.convert(3, TimeUnit.DAYS); //72
//小时转化为秒
TimeUnit.SECONDS.convert(1, TimeUnit.HOURS);//3600
Modifier and Type | Method and Description |
---|---|
long |
toDays(long duration) Equivalent to DAYS.convert(duration, this) . |
//天转化为小时
TimeUnit.DAYS.toHours(3); //72
//小时转化为秒
TimeUnit.HOURS.toSeconds(1); //3600
我正在使用ExecutorService.invokeAll(Callable, timeout, TimeUnit)方法 在提交给 ExecutorService 的每个 Callable 中,我都
我注意到 TimeUnit 类的一个奇怪行为,所以我创建了这个最小的示例来重现它。 long differenceInDays; Calendar c1 = Calendar.getInstance(
我正在尝试制作一个基本的倒数计时器。老实说,我通过教程了解了其中的大部分内容,所以我并不完全理解它的工作原理,但我已经尝试了几个小时来寻找答案,但我不知道还能去哪里寻找。 我在 TimeUnit 之后
我正在尝试研究一些方法和对能够每周使用一次的操作/“命令”的响应(防止垃圾邮件)。我唯一的问题是如何将我的java时间设置为一周。这就是我所拥有的: public static long displa
一 点睛 sleep 是一个静态方法,它有两个重载方法,其中一个需要传入毫秒数,另外一个需要传入毫秒数和纳秒数。 sleep 的方法 public static native void sleep(l
JavaAPI:https://docs.oracle.com/javase/8/docs/api/index.html 1 TimeUnit api:https://docs.oracle.com/
JavaAPI:https://docs.oracle.com/javase/8/docs/api/index.html 1 TimeUnit api:https://docs.oracle.com/
我正在使用 Python selenium 3.0.1 版,其中似乎没有说 driver.implicitly_wait(10) 我们现在必须说 driver.implicitly_wait(10,
是否有可能像 TimeUnit 这样的枚举作为参数传递给方法?我正在编写一个 Java 方法,该方法需要 long 作为持续时间参数,并且很好奇是否有更好的方法来做到这一点。基本上,我想将一些东西传递
如何为用户传递 TimeUnit 的类创建 API,例如分钟、秒、小时和一个数字,并在类内部保留毫秒值。 以下似乎是唯一的方法? void someMethodDays(int numOfDays)
我创建了这样的方法,其中有 2 个输入。 first is type (e.g. 'd', 'h') second is value (e.g. "15", "0.5") 我创建了将它转换为分钟的函数
我想将 TimeUnit 转换为 seconds 或 hours ,我问了很多但没有得到满意的答案。我读过http://developer.android.com/reference/java/uti
我正在尝试实现 Future.get(long, TimeUnit)在 TimeUnit.timedWait(Object, long) 方面. 目前尚不清楚如何使用 TimeUnit.timedWa
我试图计算两个时间戳的天数之差,TimeUnit 为我返回了完全错误的结果。 这是我的代码: long ts = 1522242239952L; long now = 1527274162820L;
我只是想知道,为什么 Java 的 TimeUnit 类中缺少一些枚举? 实际上最大时间单位是 DAY,而我想使用 WEEK、YEAR... 最佳答案 TimeUnit.DAYS 常量表示 24 小时
我希望将任意值从毫秒转换为秒,我的首选是 TimeUnit.MILLISECONDS.toSeconds。但是它返回一个长整数,因此将 1 毫秒转换为 0 秒而不是 0.001 秒。 阅读时the d
#include #include #include using namespace std; class TimeUnit { public: TimeUnit(int m, int
如何使用 Simple XML 库(版本 2.6.5/2.6.6)序列化 java.util.concurrent.TimeUnit? 这是我想要序列化的类: @Root(name="settings
我正在使用 java.util.concurrent.TimeUnit 类,我发现了一些有趣的约定,我不知道,我不明白是关于枚举的,我已经举了一个例子我自己的,我希望有人能向我解释.. 一个带有方法的
我正在尝试计算两天之间的天数差。由于某种原因,比较 01-03-2013 和 01-04-2013 给出结果 30,比较 01-03-2013 和 31-03-2013 也是如此 Calendar c
我是一名优秀的程序员,十分优秀!