Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 LocalDateTime today at specific hour

Is there a nicer/easier way of constructing LocalDateTime object representing today at 6 AM than this?

LocalDateTime todayAt6 = LocalDateTime.now().withHour(6).withMinute(0).withSecond(0).withNano(0); 

Somehow I don't like dealing with minutes/seconds/nano when all I want to say is now().withHours().

like image 829
JanM Avatar asked Apr 28 '16 12:04

JanM


People also ask

How do I set hours in LocalDateTime?

toString()); LocalDateTime ldtmdh = dateTime. withHour(12);//sets the hour of the day to 12. The other date time fields are not modified. System.

Which method of LocalDateTime can be used to get the current date and time?

now() now() method of a LocalDateTime class used to obtain the current date-time from the system clock in the default time-zone. This method will return LocalDateTime based on system clock with default time-zone to obtain the current date-time.

What timezone does LocalDateTime use?

The LocalDateTime class represents the date-time,often viewed as year-month-day-hour-minute-second and has got no representation of time-zone or offset from UTC/Greenwich.


1 Answers

LocalDate has various overloaded atTime methods, such as this one, which takes two arguments (hour of day and minute):

LocalDateTime todayAt6 = LocalDate.now().atTime(6, 0); 
like image 176
assylias Avatar answered Sep 20 '22 16:09

assylias