Saturday, November 22, 2025
HomeLanguagesJavaTemporalAdjusters nextOrSame() method in Java with Examples

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




// 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




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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11931 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6850 POSTS0 COMMENTS