Creating a Specific Date in Java

Creating a Specific Date in Java

To create a new date, you will need a Calendar instance and then you can set the Calendar instance to the date you need.

Calendar calendar = Calendar.getInstance();

The line above returns a new Calendar instance set to the current time. Calendar has many methods for changing it’s date and time or setting it outright. In our case, we’ll set it to a specific date.

calendar.set(1980, 6, 2, 8, 0, 0);Date d = calendar.getTime();

The getTime method returns the Date instance that we need. Keep in mind that the Calendar set methods only set one or more fields, they do not set them all.

See also  Java Developer Jobs: Your Guide to Landing the Perfect Position in 2024

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist