Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain the end of the day when given a LocalDate?

Tags:

java

java-time

How to obtain the end of the day when given a LocalDate?

I could get it by doing

LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59)); 

But is there an equivalent 'atStartOfDay' method for the end of the day?

LocalDate.now().atStartOfDay(); LocalDate.now().atEndOfDay(); //doesn't work 
like image 621
cooxie Avatar asked Apr 04 '16 16:04

cooxie


People also ask

How do I find the last day of the month LocalDate?

The atEndOfMonth() method of YearMonth class in Java is used to return a LocalDate of the last day of month based on this YearMonth object with which it is used.

How do I find my LocalDate hour?

The getHour() method of an LocalDateTime class is used to return the hour-of-day field. This method returns an integer value ranging from 0 to 23, i.e. the Hours of a day.

How do I get the start of a date in Java?

Getting the Start of Day Get the start of a date using atStartOfDay() method in the local timezone as well as in a specific timezone. //The date for which start of day needs to be found LocalDate localDate = LocalDate. now(); //Local date time LocalDateTime startOfDay = localDate.

How do you get day from ZonedDateTime?

The getDayOfWeek() method of a ZonedDateTime class is used to get the day-of-week field from this ZonedDateTime, which is an enum DayOfWeek. Additional information can be obtained from the DayOfWeek.


Video Answer


1 Answers

Here are a few alternatives, depending on what you need:

LocalDate.now().atTime(23, 59, 59);     //23:59:59 LocalDate.now().atTime(LocalTime.MAX);  //23:59:59.999999999 

But there is no built-in method.

As commented by @JBNizet, if you want to create an interval, you can also use an interval up to midnight, exclusive.

like image 109
assylias Avatar answered Sep 23 '22 23:09

assylias