Javatpoint Logo
Javatpoint Logo

How to Assign Static Value to Date in Java?

The java.util class in Java is used to represent dates.Date the class. Although this class has a variety of methods for manipulating dates, it does not offer a means to give a date a static value. Nevertheless, we may still accomplish this by utilising the java.time.LocalDate class, which debuted in Java 8.

The LocalDate class offers several methods for working with dates and represents a date without a time component. Use the of() function to provide a LocalDate a static value. The year, month, and day of the month are the three arguments that the of() method accepts.

Here's an example of how to assign a static value to a LocalDate:

In this example, we create a LocalDate object with the value of September 1, 2021. We then print out the date using the System.out.println() method.

We can also assign a static value to a LocalDate using a string representation of the date. The LocalDate class provides a parse() method that can parse a string representation of a date into a LocalDate object.

Here's an example of how to assign a static value to a LocalDate using a string representation of the date:

In this example, we create a LocalDate object with the value of September 1, 2021, by parsing the string "2021-09-01". We then print out the date using the System.out.println() method.

Here's a complete Java code that assigns a static value to a date using the LocalDate class and prints it out:

StaticDate.java

Output:

Static date using of() method: 2022-12-31
Static date using parse() method: 2023-05-06

The of() method, which accepts three arguments-the year, the month, and the day of the month-is used to initially set a static value to a LocalDate in the code above. The System.out.println() method is then used to print the date.

Next, we use the parse() method, which accepts a string representation of the date as an argument, to assign a static value to a LocalDate. The System.out.println() method is then used to print the date.

Both of these procedures provide a LocalDate object that represents a particular date and may be utilised in your Java code for further modification and comparison.

Here's another Java code that assigns a static value to a date using the LocalDate class and prints it out:

StaticDate.java

Output:

Static date using of() method: 2023-01-01
Static date using parse() method: 2024-05-06

In this code, we once more set static values to LocalDate objects, which stand for May 6, 2024 and January 1, 2023, respectively, by using the of() and parse() methods. The System.out.println() command is then used to print both dates.

As you can see, the output shows that both dates were correctly assigned, and it can now be utilised in Java code for comparison and further manipulation.

In conclusion, the Java class java.time can be used to set a static value to a date.Using the of() method or the parse() method, the LocalDate class can parse a string representation of the date. This makes it simple to compare and manipulate dates in Java.


Next TopicJava List Node





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA