Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavaTemporalAdjusters firstDayOfMonth() method in Java with Examples

TemporalAdjusters firstDayOfMonth() method in Java with Examples

The firstDayOfMonth() method of a TemporalAdjusters class is used to return the “first day of the month” TemporalAdjuster object, which returns a new Date object set to the first day of the current month.

Syntax:

public static TemporalAdjuster firstDayOfMonth()

Parameters: This method accepts nothing.

Return value: This method returns the first day-of-month adjuster, not null.

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




// Java program to demonstrate
// TemporalAdjusters.firstDayOfMonth()
  
import java.time.LocalDate;
import java.time.temporal.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with first day
        // of month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .firstDayOfMonth();
  
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2020, 05, 11);
        LocalDate firstDayOFMonth
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("First day for "
                           + "month for localdate "
                           + localDate + ": "
                           + firstDayOFMonth);
    }
}


Output:

First day for month for localdate 2020-05-11: 2020-05-01

Program 2:




// Java program to demonstrate
// TemporalAdjusters.firstDayOfMonth() method
  
import java.time.LocalDate;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // get TemporalAdjuster with first day
        // of month adjuster
        TemporalAdjuster temporalAdjuster
            = TemporalAdjusters
                  .firstDayOfMonth();
  
        // using adjuster for local date-time
        LocalDate localDate
            = LocalDate.of(2010, 12, 29);
        LocalDate firstDayOFMonth
            = localDate.with(temporalAdjuster);
  
        // print
        System.out.println("First day for "
                           + "month for localdate "
                           + localDate + ": "
                           + firstDayOFMonth);
    }
}


Output:

First day for month for localdate 2010-12-29: 2010-12-01

References: https://docs.oracle.com/javase/10/docs/api/java/time/format/TemporalAdjusters.html

RELATED ARTICLES

Most Popular

Recent Comments