Java LocalDate Get Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the get() method of the LocalDate class in Java to retrieve specific fields from a date. This method is useful when you only need to retrieve a specific field, such as the day of the month or day of the week, from a given date. You can use the ChronoField enum to specify which field to retrieve.

Create a Java file

Create a new file called `LocalDateGet.java` in the `~/project` directory by running the following command in your terminal:

    ```
    touch ~/project/LocalDateGet.java
    ```

Import the required packages

In your `LocalDateGet.java` file, import the required packages for working with dates:

    ```java
    import java.time.LocalDate;
    import java.time.temporal.ChronoField;
    import java.time.temporal.TemporalAccessor;
    import java.time.temporal.TemporalField;
    ```

    These packages will allow us to work with `LocalDate` objects, as well as define and retrieve specific fields from dates using the `ChronoField` and `TemporalField` enums.

Retrieve the day of the month

Create a method in `LocalDateGet` that retrieves the day of the month from a given date.

    ```java
    public static void getDayOfMonth() {
        // Take a date
        TemporalField field = ChronoField.DAY_OF_MONTH;
        TemporalAccessor date = LocalDate.of(2021,05,17);

        // Display the date
        System.out.println("Date : "+date);

        // Retrieve the day of the month using the get() method
        int val = date.get(field);

        // Display the day of the month
        System.out.println("Day of the month: "+val);
    }
    ```

    This method retrieves a date object and a `ChronoField` object representing the day of the month. It then retrieves the day of the month using the `get()` method of the `date` object and displays it.

Retrieve the day of the week

Create another method in `LocalDateGet` that retrieves the day of the week from a given date.

    ```java
    public static void getDayOfWeek() {
        // Take a date
        TemporalField field = ChronoField.DAY_OF_WEEK;
        TemporalAccessor date = LocalDate.of(2021,05,17);

        // Display the date
        System.out.println("Date : "+date);

        // Retrieve the day of the week using the get() method
        int val = date.get(field);

        // Display the day of the week
        System.out.println("Day of the week: "+val);
    }
    ```

    This method retrieves a date object and a `ChronoField` object representing the day of the week. It retrieves the day of the week using the `get()` method of the `date` object and displays it.

Retrieve the day of the year

Create one more method in `LocalDateGet` that retrieves the day of the year from a given date.

    ```java
    public static void getDayOfYear() {
        // Take a date
        TemporalField field = ChronoField.DAY_OF_YEAR;
        TemporalAccessor date = LocalDate.of(2021,05,17);

        // Display the date
        System.out.println("Date : "+date);

        // Retrieve the day of the year using the get() method
        int val = date.get(field);

        // Display the day of the year
        System.out.println("Day of the year: "+val);
    }
    ```

    This method retrieves a date object and a `ChronoField` object representing the day of the year. It then retrieves the day of the year using the `get()` method of the `date` object and displays it.

Run the program

Add the following `main()` method to your `LocalDateGet` class:

    ```java
    public static void main(String[] args) {
        getDayOfMonth();
        getDayOfWeek();
        getDayOfYear();
    }
    ```

    This method calls all three of the previous methods of the `LocalDateGet` class.

    Now, navigate to the `~/project` directory and run the following command to compile the code:

    ```
    javac LocalDateGet.java
    ```

    Once the code is compiled successfully, run the application:

    ```
    java LocalDateGet
    ```

Output

Upon executing the program, you will see the output:

    ```
    Date : 2021-05-17
    Day of the month: 17
    Date : 2021-05-17
    Day of the week: 1
    Date : 2021-05-17
    Day of the year: 137
    ```

    This output displays the date objects and the specific fields retrieved using the `get()` method of each field.

Summary

In this lab, you learned how to use the get() method of the Java LocalDate class to retrieve specific fields from dates. You created a program that retrieved the day of the month, day of the week, and day of the year for the same date. By using the get() method along with the ChronoField enum, you can make your Java programs more efficient and accurate when working with dates.

Other Java Tutorials you may like