[英]How to get a particular time in a java.util.Date in Java 8
For eg - I want 21:30 as the java.util.Date例如 - 我想要 21:30 作为 java.util.Date
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,21);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);
Date d = cal.getTime();
Is there a better way to do it in Java 8?在 Java 8 中有更好的方法吗?
I want it in java.util.Date only as I want to pass a Date in endAt() method in Quartz api while building a Trigger.我只想在 java.util.Date 中使用它,因为我想在构建触发器时在 Quartz api 中的 endAt() 方法中传递一个日期。
Conversion of the new java.time classes to java.util.Date usually go through the Instant
class.将新的 java.time 类转换为 java.util.Date 通常是 go 通过
Instant
java.Date For example, to convert LocalTime 21:30 using today's date and the system time zone you can use:例如,要使用今天的日期和系统时区转换 LocalTime 21:30,您可以使用:
LocalTime nineThirty = LocalTime.of(21, 30);
Date d = Date.from(Instant.from(nineThirty.atDate(LocalDate.now())
.atZone(ZoneId.systemDefault())))
For Quartz specifically, you may want to use the DateBuilder
class from Quartz instead of the legacy Calendar
class.特别是对于 Quartz,您可能希望使用Quartz 中的
DateBuilder
class而不是旧版Calendar
class。
Date d = DateBuilder.todayAt(21, 30, 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.