1. The LocalDate
The LocalDate class in java represents the date object. It does not include time information in it. The sole purpose of this class is to represent only the date. In this short example, we will see how to create the data object.
2. LocalDate.of Method
LocalDate.of() is a static method which takes three parameters. The parameters are:
- Year: Represents the Year in integer format
- Month: Can be represented as integer 1-12 or Enum JANUARY to DECEMBER
- Day: It is again an integer representing the Day of the month.
Now let us look at the example to see how to create date objects in Java.
3. Constructing Date
Now have a look at the below code snippet:

Code Explanation
- First, we declared two variables of type LocalDate.
- In this snippet, we use first version of the method which takes month as integer. Here, we passed o5 as the second parameter which denotes the month. We constructed the date for 31 May 2001.
- The second version of the method takes month from the Enumeration which adds more readability to the code. In this code snippet, we constructed the date for 32 Mar 2017.
Now, we have two date objects in hand. Let us see how to print them.
4. Displaying Date
We can append the date object with a string, and it will be converted into string format. In the below code, we appended both the date objects with a string and printed that in the console output window. The screenshot below shows the output as well.

5. Code Reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.time.LocalDate; import java.time.Month; import java.util.Map; import java.util.Set; public class CompareTwoDateExample { public static void main(String[] args) { //Sample 01: Declare Two Date Objects LocalDate date1; LocalDate date2; //Sample 02: Create Date 1 date1 = LocalDate.of(2001, 05, 31); //Sample 03: Create Date 2 date2 = LocalDate.of(2017, Month.MARCH, 23); //Sample 04: Now Print the Date System.out.println("First Date is: " + date1); System.out.println("Second Date is: " + date2); } } |
Categories: Java