How to find the last Saturday of the current month in Java

In this tutorial, you will learn how to find the last Saturday of the current month. That is, you will find the date on which the last Saturday of the month will fall.
You can make use of the classes LocalDate, DayOfWeek, YearMonth, and DateTimeFormatter to solve this problem.

Find the last Saturday of the current month in Java:

Steps:

  1. Obtain the current date using the now() method in the LocalDate class.
     LocalDate CurrentDate = LocalDate.now();
  2. Create a YearMonth object and pass the current year and current month as the parameters to the of() method of the YearMonth class.
    Use the getYear() method to get the current year present in the LocalDate object, CurrentDate. Similarly, use the getMonth() method to get the current month.

    YearMonth ym = YearMonth.of(CurrentDate.getYear(), CurrentDate.getMonth());
  3. Use the atEndOfMonth() method to obtain a LocalDate of the last day of the month and year specified in the YearMonth object.

    LocalDate endDate = ym.atEndOfMonth();
  4. Next, you need to find the day of the week on which the last date of the month falls. To do this, use the getDayOfWeek() method of the DayOfWeek class. This returns the day in the String format. To get the day in int format, use the getValue() method. It returns 1 for Monday, 2 for Tuesday, and so on.
    DayOfWeek day = endDate.getDayOfWeek();
    int lastDay = day.getValue();
  5. Now, if the last day falls on any day whose integer value is less than 6 (Monday – Friday), you need to subtract those many days plus 1 from the last date to get the last Saturday’s date. If the integer value is 7, that is Sunday, you need to subtract just 1 day to get the last Saturday’s date. If the integer value is 6, it means the last day falls on a Saturday itself and no calculation needs to be done. Make use of the minusDays(x) method from the LocalDate class to subtract the dates by ‘x’ number of days.
  6. Print the final date. In LocalDate, the default format of date is ‘yyyy-mm-dd’. To change this to other suitable formats like ‘dd-mm-yyyy’, make use of the format(DateTimeFormatter.ofPattern(dateFormat)) method, where ‘dateFormat’ is the suitable format of the date.

Java code:

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;

public class LastSaturday {
      public static void main(String[] args) {
      	
          LocalDate CurrentDate = LocalDate.now();  
          
          YearMonth ym = YearMonth.of(CurrentDate.getYear(), CurrentDate.getMonth());
          //YearMonth ym = YearMonth.of(2018, 9);
          LocalDate endDate = ym.atEndOfMonth();
          
          DayOfWeek day = endDate.getDayOfWeek();
          int lastDay = day.getValue();
          
          System.out.print("The last Saturday of the month falls on: ");
          
          if(lastDay < 6)
          	endDate = endDate.minusDays(lastDay+1);
          else if(lastDay > 6)
          	endDate = endDate.minusDays(1);
          
          System.out.println(endDate.format(DateTimeFormatter.ofPattern("dd-MM-yyyy")));
          
      }
}

Note:

To find the date of the last Saturday of any month and any year, uncomment the 12th line, and comment out the 11th line. Pass the required year as the first parameter, and the required month as the second parameter.

Output:

The last Saturday of the month falls on: 30-10-2021

Hope this tutorial helped! Happy coding 🙂

Also read, How to find common dates between two date ranges in Java

Leave a Reply

Your email address will not be published. Required fields are marked *