DayOfWeek adjustInto() method in Java with Examples
The adjustInto() method of java.time.DayOfWeek is an in-built function in Java which takes a Temporal object specifying a date and returns a new Temporal object of the same observable type as the input with the day-of-week changed to be the same as specified DayOfWeek constant. Note that this method adjusts forwards or backwards within a Monday to Sunday week.
Method Declaration:
public Temporal adjustInto(Temporal temporal)
Syntax:
Temporal newLocalDate = DayOfWeek.ANYWEEKDAY.adjustInto(Temporal temporal)
Parameters: This method takes temporal as parameter where:
Return Value: The function returns an adjusted Temporal object which is the date adjusted according to specified Day of the Week.
Below programs illustrate the above method:
Program 1:
import java.time.*; import java.time.DayOfWeek; import java.time.temporal.Temporal; class DayOfWeekExample { public static void main(String[] args) { // Set a Local Date whose day is found LocalDate localDate1 = LocalDate.of(1947, Month.AUGUST, 15); // Find the day from the Local Date DayOfWeek dayOfWeek1 = DayOfWeek.from(localDate1); // Printing the Local Date System.out.println(localDate1 + " which is " + dayOfWeek1.name()); // Adjust the Date to Monday from Friday Temporal localDate2 = DayOfWeek.MONDAY .adjustInto(localDate1); // Find the day from the new Local date DayOfWeek dayOfWeek2 = DayOfWeek.from(localDate2); // Printing the new Local Date System.out.println(localDate2 + " which is " + dayOfWeek2.name()); } } |
1947-08-15 which is FRIDAY 1947-08-11 which is MONDAY
Program 2:
import java.time.*; import java.time.DayOfWeek; import java.time.temporal.Temporal; class DayOfWeekExample { public static void main(String[] args) { // Set a Local Date whose day is found LocalDate localDate1 = LocalDate.of(2019, Month.MARCH, 18); // Find the day from the Local Date DayOfWeek dayOfWeek1 = DayOfWeek.from(localDate1); // Printing the Local Date System.out.println(localDate1 + " which is " + dayOfWeek1.name()); // Adjust the Date to Wednesday from Monday Temporal localDate2 = DayOfWeek.WEDNESDAY .adjustInto(localDate1); // Find the day from the new Local date DayOfWeek dayOfWeek2 = DayOfWeek.from(localDate2); // Printing the new Local Date System.out.println(localDate2 + " which is " + dayOfWeek2.name()); } } |
2019-03-18 which is MONDAY 2019-03-20 which is WEDNESDAY
Recommended Posts:
- DayOfWeek of() method in Java with Examples
- DayOfWeek plus() method in Java with Examples
- DayOfWeek get() method in Java with Examples
- DayOfWeek from() method in Java with Examples
- OffsetTime adjustInto() method in Java with examples
- LocalTime adjustInto() method in Java with Examples
- ChronoLocalDate adjustInto() method in Java with Examples
- ChronoLocalDateTime adjustInto() method in Java with Examples
- Year adjustInto() Method in Java with Examples
- OffsetDateTime adjustInto() method in Java with examples
- MonthDay adjustInto() method in Java with Examples
- DayOfWeek getValue() method in Java with Examples
- DayOfWeek getDisplayName() method in Java with Examples
- DayOfWeek valueOf() method in Java with Examples
- DayOfWeek values() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



