TemporalAdjusters nextOrSame() method in Java with Examples
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
import java.time.*;
import java.time.temporal.*;
public class GFG {
public static void main(String[] args)
{
TemporalAdjuster temporalAdjuster
= TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY);
LocalDate localDate
= LocalDate.of( 2020 , 10 , 31 );
LocalDate nextOrSameDOW
= localDate.with(temporalAdjuster);
System.out.println("Next or same date having "
+ "WEDNESDAY for localdate "
+ localDate + " is: "
+ nextOrSameDOW);
}
}
|
Output:
Next or same date having WEDNESDAY for localdate 2020-10-31 is: 2020-11-04
Program 2:
Java
import java.time.*;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
public class GFG {
public static void main(String[] args)
{
TemporalAdjuster temporalAdjuster
= TemporalAdjusters
.nextOrSame(
DayOfWeek.THURSDAY);
LocalDate localDate
= LocalDate.of( 2020 , 02 , 27 );
LocalDate nextOrSameDOW
= localDate.with(temporalAdjuster);
System.out.println("Next or same date having "
+ "WEDNESDAY for localdate "
+ localDate + " is: "
+ nextOrSameDOW);
}
}
|
Output:
Next or same date having WEDNESDAY for localdate 2020-02-27 is: 2020-02-27
References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/TemporalAdjusters.html#nextOrSame(java.time.DayOfWeek)
Last Updated :
27 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...