Close

Java Date Time - DayOfWeek.adjustInto() Examples

Java Date Time Java Java API 


Class:

java.time.DayOfWeek

java.lang.Objectjava.lang.Objectjava.lang.Enumjava.lang.Enumjava.lang.ComparableComparablejava.io.SerializableSerializablejava.time.DayOfWeekjava.time.DayOfWeekjava.time.temporal.TemporalAccessorTemporalAccessorjava.time.temporal.TemporalAdjusterTemporalAdjusterLogicBig

Method:

Temporal adjustInto(Temporal temporal)

DayOfWeek#adjustInto adjusts the provided temporal object to this day-of-week.

This method adjusts the provided temporal object forwards or backwards within a Monday to Sunday week (week starts from MONDAY and ends at SUNDAY).

If this DayOfWeek is before day field of the provided temporal then resultant temporal will be pushed backwards otherwise forward.


Examples


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.Temporal;

public class AdjustIntoExample {
public static void main (String[] args) {
LocalDate dateTime = LocalDate.of(2014, 11, 2);
Temporal t = dateTime.getDayOfWeek()
.adjustInto(LocalDateTime.now());
System.out.println(t);
}
}

Output

2017-05-07T15:54:25.364




In this example the resultant temporal is pushed backwards from the provided localDate.

public class AdjustIntoExample2 {
public static void main (String[] args) {
LocalDate localDate = LocalDate.of(2016, 11, 30);
Temporal t = DayOfWeek.MONDAY
.adjustInto(localDate);
System.out.println(t);
}
}

Output

2016-11-28




In this example the resultant temporal is pushed forward from the provided localDate because SATURDAY is still ahead of the day portion of 2016-11-01 which is Tuesday.

public class AdjustIntoExample3 {
public static void main (String[] args) {
LocalDate localDate = LocalDate.of(2016, 11, 01);
System.out.println("day in localDate: " + localDate.getDayOfWeek());
Temporal t = DayOfWeek.SATURDAY
.adjustInto(localDate);
System.out.println(t);
}
}

Output

day in localDate: TUESDAY
2016-11-05




See Also