Friday, May 10, 2024
HomeLanguagesJavaTemporalAdjusters previous() method in Java with Examples

TemporalAdjusters previous() method in Java with Examples

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

public static TemporalAdjuster previous(DayOfWeek dayOfWeek)

Parameters: This method accepts dayOfWeek which can be used to get a new Date object which is the previous date with the same matching DayOfWeek. Return value: This method returns the previous day of the week adjuster. Below programs illustrate the TemporalAdjusters.previous() method: Program 1: 

Java




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


Output:

Previous day of the week having WEDNESDAY for localdate 1998-10-31 is: 1998-10-28

Program 2: 

Java




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


Output:

previous day of the week having THURSDAY for localdate 2029-12-11 is: 2029-12-06

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

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments