RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Tip: Creating a Specific Date in Java

Learn how to create a calendar instance and set a specific date.


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.


Visit the DevX Tip Bank


Octavia Andreea Anghel is a senior PHP developer currently working as a primary trainer for programming teams that participate at national and international software-development contests. She consults on developing educational projects at a national level. She is a coauthor of the book "XML Technologies--XML in Java" (Albastra, ISBN 978-973-650-210-1), for which she wrote the XML portions. In addition to PHP and XML, she's interested in software architecture, web services, UML, and high-performance unit tests.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date