Setting Dates With Calendar and GregorianCalendar

In Java 1.0.2, the java.util.Date class was used to represent a point in time, as well as to parse and format date values. However, as part of the internationalization features introduced with Java 1.1, most of the methods in Date were deprecated, and the java.util.Calendar and java.util.GregorianCalendar classes were added. Calendar is an abstract class and cannot be instantiated, while GregorianCalendar is a concrete subclass of Calendar that’s often useful when working with date and/or time values. There are a number of GregorianCalendar constructors that you can use when creating date/time representations. For example, to create an instance that corresponds to July 4, 1776, use:

 GregorianCalendar grc =new GregorianCalendar(1776, Calendar.JULY, 4);

You can also create an instance that includes a specific time value, such as 12:34:56 as follows:

 GregorianCalendar grc =new GregorianCalendar(1776, Calendar.JULY, 4, 12, 34, 56);

The Calendar and GregorianCalendar classes provide many useful methods for such functions as date arithmetic, identifying leap years, and so forth.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: