Angle Bracket <> in Java with Examples Different types of Recursions in Java Java Lambda Filter Example Java Program for Shopping Bill Pythagorean Triplet with Given Sum Using Single Loop in Java TemporalAdjusters lastInMonth() method in Java with Examples ZIP API in Java Atomic reference in Java Digit Extraction in Java DRY (Don't Repeat Yourself) Principles in Java with Examples Empty array in Java Is main method compulsory in Java? Java I/O Operation - Wrapper Class vs Primitive Class Variables Java Program to Find the Perimeter of a Rectangle Java Thread Priority Java Type Erasure Need of Concurrent Collections in Java Nested ArrayList in Java Print Odd and Even Numbers by Two Threads in Java Square pattern in Java TemporalAdjusters next() method in Java with Examples What does start() function do in multithreading in Java Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples JDBC MySQL Localhost 3306 Alternate Vowel and Consonant string in Java Built-in Exceptions in Java with Examples Capture the Pawns Problem in Java Collections.shuffle() Method in Java with Examples Convert Number to Words Problem in Java Detect And Remove Cycle in a Single Linked List in Java Evolution of Interfaces in Java How to pad a String in Java Implementing Patricia Trie in Java Java Program to Find the Most Repeated Word in a Text File java.util.UUID class in Java ReadWriteLock Interface in Java Reference Data Types in Java Sort An Array According to The Count of Set Bits in Java JDBC MySQL Localhost 3306 Adding a Character as Thousands Separator to Given Number in Java Circular Primes in Java Equilibrium Index Problem in Java Java String regionMatches() Method with Examples LFU Cache Problem in Java Longest Repeating and Non-Overlapping Substring in Java Prefix Suffix Search in Java Product Array Puzzle Problem in Java Russian Doll Envelopes Problem in Java Second Most Repeated Word in a Sequence in Java Special Two-Digit Numbers in a Binary Search Tree in Java Swap Corner Words and Reverse Middle Characters in Java Toggle K bits Problem in Java Upside Down Binary Tree in Java Verbal Arithmetic Puzzle Problem in Java Insert a String into another String in Java Print Shortest Path to Print a String on Screen in Java Search Insert Position in Java BST Sequences in Java Burrows - Wheeler Data Transform Algorithm in Java Convert BST to Min Heap in Java Fibonacci Sum in Java Get name of current method being executed in Java

TemporalAdjusters next() method in Java with Examples

One essential tool for changing temporal objects is an adjuster. They allow for various approaches based on the strategy design pattern and serve to externalize the process of adjustment. Some examples of date adjusters would be ones that fix the date to the final day of the month or to avoid weekends.

A temporal adjuster can be used in two compatible methods. One way to start is by directly calling the method on the interface. Using Temporal.with(TemporalAdjuster) is the second option.

// While these two lines are similar, it is best to choose the second strategy.

temporal = thisAdjuster.adjustInto(temporal);

temporal = temporal.with(thisAdjuster);

One should use the second strategy, which uses a temporal adjuster, since the code is much easier to understand. Standard adjusters are included in this class and can be accessed by static methods.

Example 1:

Input:

var d1 = localDate.with(Temporal.firstDayOfMonth());

Output:

today : 2023-12-03

Explanation:

We can determine the first day of the month using firstDayOfMonth.

Example 2:

Input:

var d2 = localDate.with(Temporal.firstDayOfYear());

Output:

first day of year : 2023-01-01

Explanation:

We can identify the first day of the year using firstDayOfYear.

Example 3:

Input:

var d3 = localDate.with(Temporal.firstDayOfNextMonth());

Output:

first day of next month : 2024-01-01

Explanation:

We could find the first day of the following month by using firstDayOfNextMonth.

Summary of Some Methods

  • dayOfWeekInMonth() - an adjuster-for the ordinary day of the week.
  • firstDayOfMonth() - an adjuster-for the current month's first day of the month.
  • firstDayOfNextMonth() - an adjuster-for the date of the following month's first day.
  • firstDayOfNextYear() - a date adjuster for the first day of the upcoming year
  • firstDayOfYear() - an adjuster-for the date of the current year's first day.
  • lastDayOfMonth() - an adjuster-for the date of the current year's first day.
  • nextOrSame() - a date adjuster for the following occurrence of a particular day of the week, or the same day if today falls on the needed day of the week.

The next(DayOfWeek) method of a TemporalAdjusters class is used to return a TemporalAdjuster object representing the next day of the week. This TemporalAdjuster object can then be used to obtain a new Date object, which is the date that follows any Date object to that this TemporalAdjuster is applied to, and which is the next date with the same matching DayOfWeek as passed as a parameter.

Syntax:

public static TemporalAdjuster next(DayOfWeek dayOfWeek)

Parameters: The next date with the same corresponding DayOfWeek is the new Date object that can be obtained by using the dayOfWeek parameter that this method requires.

Return value: Rather than returning null, this function returns the adjuster for the following day of the week.

Implementation:

FileName: TemporalExample.java

import java.io.*;

import java.util.*;

import java.time.*;

import java.time.temporal.*;

public class TemporalExample

{

            public static void main(String[] args)

            {

                // get TemporalAdjuster using

                        // the following in-month adjuster

                        TemporalAdjuster temporal = TemporalAdjusters.next(DayOfWeek.SUNDAY);

        // utilising a local date time adjuster

                        LocalDate localDate = LocalDate.of(2023, 12, 03);

                        LocalDate next = localDate.with(temporal);

                        // returns the date

                        System.out.println("The following day of the week after" + " SUNDAY for localdate " + localDate + " is given by: " + next);

            }

}

Output:

The following day of the week after SUNDAY for localdate 2023-12-03 is given by: 2023-12-10