TemporalAdjusters previousOrSame() method in Java with Examples

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

Syntax:

public static TemporalAdjuster
       previousOrSame(DayOfWeek dayOfWeek)

Parameters: This method accepts dayOfWeek which can be used to get a new Date object which is the previous or same date with the same matching day-of-week as passed. It should not be null.

Return value: This method return a previous or same day-of-week TemporalAdjuster object.

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



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// TemporalAdjusters.previousOrSame()
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get TemporalAdjuster with the
        // previousOrSame() of month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .previousOrSame(
                      DayOfWeek.FRIDAY);
  
        // Using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2020, 10, 30);
        LocalDate previousOrSameDOW
            = localDate.with(temporalAdjuster);
  
        // Print
        System.out.println(
            "Previous or same date "
            + "having FRIDAY for localdate "
            + localDate + " is: "
            + previousOrSameDOW);
    }
}

chevron_right


Output:

Previous or same date having FRIDAY for localdate 2020-10-30 is: 2020-10-30

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// TemporalAdjusters previousOrSame() method
  
import java.time.*;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Get TemporalAdjuster with the
        // previousOrSame() day of week adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .previousOrSame(
                      DayOfWeek.THURSDAY);
  
        // Using adjuster for local date time
        LocalDate localDate
            = LocalDate.of(2018, 02, 23);
        LocalDate previousOrSameDOW
            = localDate.with(temporalAdjuster);
  
        // Print
        System.out.println(
            "Previous or same date "
            + "having THURSDAY for localdate "
            + localDate + " is: "
            + previousOrSameDOW);
    }
}

chevron_right


Output:

Previous or same date having THURSDAY for localdate 2018-02-23 is: 2018-02-22

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

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.