If you want to hardcode the value for a date in Java then you can make use of the java.time.LocalDate (Java 8 and above) or java.util.Date (pre-Java 8)
Let's take a look at some examples.
Example 1: Using Java 8 or above java.time.LocalDate
LocalDate date = LocalDate.of(2023, 12, 25);
Example 2: Using Java 8 or above Instant
Instant instant = Instant.parse("2023-12-25T00:00:00.00Z");
Example 3: Using Java 6 or above Calendar
Calendar calendar = Calendar.getInstance();
calendar.set(2023, 11, 25);
Example 4: Using Java 7 or before SimpleDateFormat
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = dateFormat.parse("2023-12-25");
Example 5: Using Java 7 or before java.util.Date
Date date = new Date(123, 11, 25);
Preferred way!

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!