Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

TemporalAdjusters next() method in Java with Examples

  • Last Updated : 27 Mar, 2020

The next(DayOfWeek) method of a TemporalAdjusters class is used to return a next day-of-week TemporalAdjuster object which can be used to get a new Date object which is the next date with the same matching DayOfWeek as passed as a parameter from any Date object on which this TempotralAdjuster is applied.

Syntax:

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.

public static TemporalAdjuster next(DayOfWeek dayOfWeek)

Parameters: This method accepts dayOfWeek which can be used to get a new Date object which is the next date with the same matching DayOfWeek.

Return value: This method returns the next day of the week adjuster, not null.



Below programs illustrate the TemporalAdjusters.next() method:
Program 1:




// Java program to demonstrate
// TemporalAdjusters.next()
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with
        // the next in month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters.next(
                DayOfWeek.WEDNESDAY);
  
        // using adjuster for local date time
        LocalDate localDate
            = LocalDate.of(1998, 10, 31);
        LocalDate nextDOW
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println(
            "next day of the week having"
            + " WEDNESDAY for localdate "
            + localDate + " is: "
            + nextDOW);
    }
}
Output:
next day of the week having WEDNESDAY for localdate 1998-10-31 is: 1998-11-04

Program 2:




// Java program to demonstrate
// TemporalAdjusters.next() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with the
        // next day of week adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters.next(
                DayOfWeek.THURSDAY);
  
        // using adjuster for local date time
        LocalDate localDate
            = LocalDate.of(2029, 12, 11);
        LocalDate nextDOW
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println(
            "next day of the week having"
            + " THURSDAY for localdate "
            + localDate + " is: "
            + nextDOW);
    }
}
Output:
next day of the week having THURSDAY for localdate 2029-12-11 is: 2029-12-13

References: https://docs.oracle.com/javase/10/docs/api/java/time/temporal/TemporalAdjusters.html#next(java.time.DayOfWeek)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!