Skip to content
Related Articles

Related Articles

TemporalAdjusters nextOrSame() method in Java with Examples
  • Last Updated : 27 Mar, 2020

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 TempotralAdjuster 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 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);
    }
}
Output:
Next or same date having WEDNESDAY for localdate 2020-10-31 is: 2020-11-04

Program 2:




// 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);
    }
}
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)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :