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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With