I have the following Joda-Time code which sets the weekday:
LocalDateTime localDateTime = new LocalDateTime(2016, 1, 1, 20,39);
LocalDateTime localDateTime1 = localDateTime.withDayOfWeek(7);
How can I do the same with java.time
? I see that there is no setDayOfTheWeek
:
LocalDateTime time;
time.getDayOfWeek()
Use time.with(TemporalAdjuster)
, specifying an instance of DayOfWeek
, e.g.
LocalDateTime time1 = time.with(DayOfWeek.FRIDAY);
It has no setDayOfTheWeek
because LocalDateTime
is immutable, so it has no setters to modify the object. (Joda Time classes are also immutable!).
Use this in Java 8:
LocalDateTime localDateTime = LocalDateTime.of(2016, 1, 1, 20,39);
LocalDateTime localDateTime1 = localDateTime.with(DayOfWeek.SUNDAY);
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