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:
temporal – is the specified date to be adjusted.
ANYWEEKDAY – is the specified day to which the date is to be adjusted, e.g., MONDAY, TUESDAY, etc.
newLocalDate – is the modified date.
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)
{
LocalDate localDate1
= LocalDate.of( 1947 , Month.AUGUST, 15 );
DayOfWeek dayOfWeek1
= DayOfWeek.from(localDate1);
System.out.println(localDate1
+ " which is "
+ dayOfWeek1.name());
Temporal localDate2
= DayOfWeek.MONDAY
.adjustInto(localDate1);
DayOfWeek dayOfWeek2
= DayOfWeek.from(localDate2);
System.out.println(localDate2
+ " which is "
+ dayOfWeek2.name());
}
}
|
Output:
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)
{
LocalDate localDate1
= LocalDate.of( 2019 , Month.MARCH, 18 );
DayOfWeek dayOfWeek1
= DayOfWeek.from(localDate1);
System.out.println(localDate1
+ " which is "
+ dayOfWeek1.name());
Temporal localDate2
= DayOfWeek.WEDNESDAY
.adjustInto(localDate1);
DayOfWeek dayOfWeek2
= DayOfWeek.from(localDate2);
System.out.println(localDate2
+ " which is "
+ dayOfWeek2.name());
}
}
|
Output:
2019-03-18 which is MONDAY
2019-03-20 which is WEDNESDAY
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#adjustInto-java.time.temporal.Temporal-
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
19 Mar, 2019
Like Article
Save Article