Java 8 LocalDateTime today at specific hour

JanM picture JanM · Apr 28, 2016 · Viewed 41k times · Source

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().

Answer

assylias picture assylias · Apr 28, 2016

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);