The nextOrSame(DayOfWeek) method of a TemporalAdjusters class is used to return a next-or-same DayOfWeek TemporalAdjuster object which can be used to get a new Date object which is the next-or-same date with the same DayOfWeek specified as the parameter, from any Date object on which this TemporalAdjuster is applied. Syntax:
public static TemporalAdjuster nextOrSame(DayOfWeek dayOfWeek)
Parameters: This method accepts dayOfWeek which can be used to get a new Date object which is the next-or-same date with the same DayOfWeek Return value: This method returns the next-or-same DayOfWeek adjuster. Below programs illustrate the TemporalAdjusters.nextOrSame() method: Program 1:
Java
// Java program to demonstrate // TemporalAdjusters.nextOrSame() import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // get TemporalAdjuster with the nextOrSame in // month adjuster TemporalAdjuster temporalAdjuster = TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY); // using adjuster for local date time LocalDate localDate = LocalDate.of( 2020 , 10 , 31 ); LocalDate nextOrSameDOW = localDate.with(temporalAdjuster); // print System.out.println("Next or same date having " + "WEDNESDAY for localdate " + localDate + " is: " + nextOrSameDOW); } } |
Next or same date having WEDNESDAY for localdate 2020-10-31 is: 2020-11-04
Program 2:
Java
// Java program to demonstrate // TemporalAdjusters.nextOrSame() method import java.time.*; import java.time.temporal.TemporalAdjuster; import java.time.temporal.TemporalAdjusters; public class GFG { public static void main(String[] args) { // get TemporalAdjuster with the // nextOrSame day of week adjuster TemporalAdjuster temporalAdjuster = TemporalAdjusters .nextOrSame( DayOfWeek.THURSDAY); // using adjuster for local date time LocalDate localDate = LocalDate.of( 2020 , 02 , 27 ); LocalDate nextOrSameDOW = localDate.with(temporalAdjuster); // print System.out.println("Next or same date having " + "WEDNESDAY for localdate " + localDate + " is: " + nextOrSameDOW); } } |
Next or same date having WEDNESDAY for localdate 2020-02-27 is: 2020-02-27