Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jodatime start of day and end of day

I want to create an interval between the beginning of the week, and the end of the current week.

I have the following code, borrowed from this answer:

private LocalDateTime calcNextSunday(LocalDateTime d) {     if (d.getDayOfWeek() > DateTimeConstants.SUNDAY) {         d = d.plusWeeks(1);     }     return d.withDayOfWeek(DateTimeConstants.SUNDAY); }  private LocalDateTime calcPreviousMonday(LocalDateTime d) {     if (d.getDayOfWeek() < DateTimeConstants.MONDAY) {         d = d.minusWeeks(1);     }     return d.withDayOfWeek(DateTimeConstants.MONDAY); } 

But now I want the Monday LocalDateTime to be at 00:00:00, and the Sunday LocalDateTime at 23:59:59. How would I do this?

like image 551
nhaarman Avatar asked Feb 05 '12 21:02

nhaarman


People also ask

How to get end of day time using java?

From a LocalDate Object of(localDate, LocalTime. MIDNIGHT); LocalTime offers the following static fields: MIDNIGHT (00:00), MIN (00:00), NOON (12:00), and MAX(23:59:59.999999999). Therefore, if we want to get the end of the day, we'd use the MAX value.

What is Joda time?

Joda-Time is the most widely used date and time processing library, before the release of Java 8. Its purpose was to offer an intuitive API for processing date and time and also address the design issues that existed in the Java Date/Time API.

What is zoned date time?

ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.


1 Answers

You can use the withTime method:

 d.withTime(0, 0, 0, 0);  d.withTime(23, 59, 59, 999); 

Same as Peter's answer, but shorter.

like image 62
JodaStephen Avatar answered Sep 21 '22 18:09

JodaStephen