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.
Open the terminal in your WebIDE. The default directory is
~/project.Create the
LocalDateGet.javafile using thetouchcommand:touch LocalDateGet.javaThis command creates an empty file in your current directory. You can verify its creation by listing the files:
lsYou should see
LocalDateGet.javalisted 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.
Open the
LocalDateGet.javafile in the WebIDE's code editor.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 asDAY_OF_MONTH,DAY_OF_WEEK,DAY_OF_YEAR, etc.java.time.temporal.TemporalAccessor: This interface provides read-only access to a temporal object, such asLocalDate. Theget()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.ChronoFieldimplements this interface.
Save the
LocalDateGet.javafile 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.
Open the
LocalDateGet.javafile in the WebIDE's code editor.Add the
getDayOfMonth()method inside theLocalDateGetclass, but outside themainmethod: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 aLocalDateobject 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 theget()method on thedateobject, passing thefield(which isChronoField.DAY_OF_MONTH) to extract the corresponding integer value.
Save the
LocalDateGet.javafile.Now, let's call this method from
mainto see it in action. Modify yourmainmethod as follows:public static void main(String[] args) { getDayOfMonth(); // Other methods will be called here later }Save the file again.
Open the terminal and compile your Java code:
javac LocalDateGet.javaIf there are no errors, the compilation will complete silently.
Run the compiled Java program:
java LocalDateGetYou should see output similar to this:
Date : 2021-05-17 Day of the month: 17This 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.
Open the
LocalDateGet.javafile in the WebIDE's code editor.Add the
getDayOfWeek()method inside theLocalDateGetclass, below thegetDayOfMonth()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) ... } }Save the
LocalDateGet.javafile.Now, call this new method from your
mainmethod. Update themainmethod as follows:public static void main(String[] args) { getDayOfMonth(); getDayOfWeek(); // Call the new method // Other methods will be called here later }Save the file again.
Compile your Java code from the terminal:
javac LocalDateGet.javaRun the compiled Java program:
java LocalDateGetYou should see output similar to this:
Date : 2021-05-17 Day of the month: 17 Date : 2021-05-17 Day of the week: 1Since May 17, 2021, was a Monday, and
ChronoField.DAY_OF_WEEKrepresents Monday as 1, the outputDay of the week: 1is 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.
Open the
LocalDateGet.javafile in the WebIDE's code editor.Add the
getDayOfYear()method inside theLocalDateGetclass, below thegetDayOfWeek()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) ... } }Save the
LocalDateGet.javafile.Now, call this final method from your
mainmethod. Update themainmethod as follows:public static void main(String[] args) { getDayOfMonth(); getDayOfWeek(); getDayOfYear(); // Call the new method }Save the file again.
Compile your Java code from the terminal:
javac LocalDateGet.javaRun the compiled Java program:
java LocalDateGetYou 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: 137May 17, 2021, was the 137th day of the year, so the output
Day of the year: 137is 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.



