Java LocalDate Get Method

JavaBeginner
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

In this step, you will create a new Java file named LocalDateGet.java in your project directory. This file will contain the code to demonstrate the get() method of the LocalDate class.

  1. Open the terminal in your WebIDE. The default directory is ~/project.

  2. Create the LocalDateGet.java file using the touch command:

    touch LocalDateGet.java
    

    This command creates an empty file in your current directory. You can verify its creation by listing the files:

    ls
    

    You should see LocalDateGet.java listed in the output.

Add necessary imports and class structure

Now, you will add the basic structure to your LocalDateGet.java file, including the required import statements and the main class definition. These imports are essential for working with date and time APIs in Java.

  1. Open the LocalDateGet.java file in the WebIDE's code editor.

  2. Add the following code to the file:

    import java.time.LocalDate;
    import java.time.temporal.ChronoField;
    import java.time.temporal.TemporalAccessor;
    import java.time.temporal.TemporalField;
    
    public class LocalDateGet {
    
        public static void main(String[] args) {
            // This is where we will call our methods later
        }
    }
    
    • java.time.LocalDate: This class represents a date without a time-zone.
    • java.time.temporal.ChronoField: This enum provides a standard set of fields for date and time, such as DAY_OF_MONTH, DAY_OF_WEEK, DAY_OF_YEAR, etc.
    • java.time.temporal.TemporalAccessor: This interface provides read-only access to a temporal object, such as LocalDate. The get() method is defined in this interface.
    • java.time.temporal.TemporalField: This interface represents a field of date-time, such as year, month, or day-of-month. ChronoField implements this interface.
  3. Save the LocalDateGet.java file after adding the code.

Retrieve the day of the month

In this step, you will add a method to your LocalDateGet class that demonstrates how to retrieve the day of the month from a LocalDate object using the get() method and ChronoField.DAY_OF_MONTH.

  1. Open the LocalDateGet.java file in the WebIDE's code editor.

  2. Add the getDayOfMonth() method inside the LocalDateGet class, but outside the main method:

    import java.time.LocalDate;
    import java.time.temporal.ChronoField;
    import java.time.temporal.TemporalAccessor;
    import java.time.temporal.TemporalField;
    
    public class LocalDateGet {
    
        public static void getDayOfMonth() {
            // Define the field to retrieve: DAY_OF_MONTH
            TemporalField field = ChronoField.DAY_OF_MONTH;
            // Create a LocalDate object for May 17, 2021
            TemporalAccessor date = LocalDate.of(2021, 5, 17);
    
            // Display the original date
            System.out.println("Date : " + date);
    
            // Retrieve the day of the month using the get() method
            int val = date.get(field);
    
            // Display the retrieved day of the month
            System.out.println("Day of the month: " + val);
            System.out.println(); // Add a blank line for better readability
        }
    
        public static void main(String[] args) {
            // This is where we will call our methods later
        }
    }
    
    • LocalDate.of(2021, 5, 17): Creates a LocalDate object representing May 17, 2021.
    • ChronoField.DAY_OF_MONTH: Specifies that we want to retrieve the day of the month.
    • date.get(field): This is the core of the operation. It calls the get() method on the date object, passing the field (which is ChronoField.DAY_OF_MONTH) to extract the corresponding integer value.
  3. Save the LocalDateGet.java file.

  4. Now, let's call this method from main to see it in action. Modify your main method as follows:

    public static void main(String[] args) {
        getDayOfMonth();
        // Other methods will be called here later
    }
    
  5. Save the file again.

  6. Open the terminal and compile your Java code:

    javac LocalDateGet.java
    

    If there are no errors, the compilation will complete silently.

  7. Run the compiled Java program:

    java LocalDateGet
    

    You should see output similar to this:

    Date : 2021-05-17
    Day of the month: 17
    

    This confirms that the getDayOfMonth() method correctly extracts the day of the month.

Retrieve the day of the week

Next, you will add another method to retrieve the day of the week. The ChronoField.DAY_OF_WEEK field represents the day of the week, where 1 is Monday, 2 is Tuesday, and so on, up to 7 for Sunday.

  1. Open the LocalDateGet.java file in the WebIDE's code editor.

  2. Add the getDayOfWeek() method inside the LocalDateGet class, below the getDayOfMonth() method:

    import java.time.LocalDate;
    import java.time.temporal.ChronoField;
    import java.time.temporal.TemporalAccessor;
    import java.time.temporal.TemporalField;
    
    public class LocalDateGet {
    
        // ... (getDayOfMonth method as defined previously) ...
    
        public static void getDayOfWeek() {
            // Define the field to retrieve: DAY_OF_WEEK
            TemporalField field = ChronoField.DAY_OF_WEEK;
            // Create a LocalDate object for May 17, 2021 (which was a Monday)
            TemporalAccessor date = LocalDate.of(2021, 5, 17);
    
            // Display the original date
            System.out.println("Date : " + date);
    
            // Retrieve the day of the week using the get() method
            int val = date.get(field);
    
            // Display the retrieved day of the week
            System.out.println("Day of the week: " + val);
            System.out.println(); // Add a blank line for better readability
        }
    
        public static void main(String[] args) {
            // ... (main method as defined previously) ...
        }
    }
    
  3. Save the LocalDateGet.java file.

  4. Now, call this new method from your main method. Update the main method as follows:

    public static void main(String[] args) {
        getDayOfMonth();
        getDayOfWeek(); // Call the new method
        // Other methods will be called here later
    }
    
  5. Save the file again.

  6. Compile your Java code from the terminal:

    javac LocalDateGet.java
    
  7. Run the compiled Java program:

    java LocalDateGet
    

    You should see output similar to this:

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

    Since May 17, 2021, was a Monday, and ChronoField.DAY_OF_WEEK represents Monday as 1, the output Day of the week: 1 is correct.

Retrieve the day of the year

Finally, you will add a method to retrieve the day of the year. The ChronoField.DAY_OF_YEAR field represents the day of the year, where 1 is January 1st, and so on.

  1. Open the LocalDateGet.java file in the WebIDE's code editor.

  2. Add the getDayOfYear() method inside the LocalDateGet class, below the getDayOfWeek() method:

    import java.time.LocalDate;
    import java.time.temporal.ChronoField;
    import java.time.temporal.TemporalAccessor;
    import java.time.temporal.TemporalField;
    
    public class LocalDateGet {
    
        // ... (getDayOfMonth and getDayOfWeek methods as defined previously) ...
    
        public static void getDayOfYear() {
            // Define the field to retrieve: DAY_OF_YEAR
            TemporalField field = ChronoField.DAY_OF_YEAR;
            // Create a LocalDate object for May 17, 2021
            TemporalAccessor date = LocalDate.of(2021, 5, 17);
    
            // Display the original date
            System.out.println("Date : " + date);
    
            // Retrieve the day of the year using the get() method
            int val = date.get(field);
    
            // Display the retrieved day of the year
            System.out.println("Day of the year: " + val);
            System.out.println(); // Add a blank line for better readability
        }
    
        public static void main(String[] args) {
            // ... (main method as defined previously) ...
        }
    }
    
  3. Save the LocalDateGet.java file.

  4. Now, call this final method from your main method. Update the main method as follows:

    public static void main(String[] args) {
        getDayOfMonth();
        getDayOfWeek();
        getDayOfYear(); // Call the new method
    }
    
  5. Save the file again.

  6. Compile your Java code from the terminal:

    javac LocalDateGet.java
    
  7. Run the compiled Java program:

    java LocalDateGet
    

    You should see the complete output, including the day of the year:

    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
    

    May 17, 2021, was the 137th day of the year, so the output Day of the year: 137 is correct.

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 (May 17, 2021). By using the get() method along with the ChronoField enum, you can efficiently extract specific date components, making your Java programs more precise when working with temporal data.