Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to get a particular time in a java.util.Date in Java 8 without wasting too much if your time.
The question is published on by Tutorial Guruji team.
The question is published on by Tutorial Guruji team.
For eg – I want 21:30 as the 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 ?
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.
Answer
Conversion of the new java.time classes to java.util.Date usually go through the Instant
class. For example, to convert LocalTime 21:30 using today’s date and the system time zone you can use:
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.
Date d = DateBuilder.todayAt(21, 30, 0);
We are here to answer your question about How to get a particular time in a java.util.Date in Java 8 - If you find the proper solution, please don't forgot to share this with your team members.