The Date class is used to represent a Date and time. We can manipulate  the day, month ,year, day-of-the-week, hours, minutes, and seconds. There are several constructors for Date objects. The simple one, Date (), initializes the object with the current date and time. Most of this class methods have been deprecated . Its API design didnt do a good job of handling internationalization and localization situations. The Date class is used as temporary bridge to format a Calender object using the DateFormat class. Three more constructors offer increasing level of specificity  for how accurate we would like the time to be.

 

  Date(year,month,date)will set the time to 00:00:00(midnight) on the specified day.

  Date(year, month,date,hours,minutes)will set the date and time,leaving the seconds set to 0.

  Date(year,month,date,hours, minutes,seconds) will exactly specify the time.

 

            The  last constructor for Date will parse  a String representation of the date and time into an object. Date contains a static method called parse which can be called directly to parse a String formated date directly into the  long representation; parse is used by this constructor. Date parses many formats of String dates.

 

            Date includes set of methods for getting and setting the year,month,day hour, minute and seconds of an instance. You can only get the day-of-the-week, since it is bound to the day of the month. Each of the get functions -getYear, getMonth, getDate, getDay, getHours, getMinutes, and getSeconds-returns an int. Each of the set methods-setYear, setMonth, setDate, setHours, setMinute, and setSeconds-take an int parameter. We can get the long representation of  Date object with the getTime method. This the number of milliseconds since January 1,1970. We can reconstruct a Date Object some time later by passing in this long to the Date constructor, or we can change a Date object by calling setTime with a new long.

 

            If we have two Date objects and we want to compare , we could convert them to their long representation to compare them as integer numbers of milliseconds. The Date class includes three methods that can be used for direct comparisonbefore,after, and equals. For example new Date(96,2,18).before(new Date(96,2,12)) returns true, because the 12th came before the 18th. Here are the three method signatures:

  boolean before(Date when)

  boolean after(Date when)

  boolean equal(Date other)

 

            Date objects can be converted to String representations in  several forms. First, the normal toString method converts Date object to a String that looks like this : Thu Feb 15 22:42:04 2011. Then, the toLocaleString method converts Date to a shorter String like this: 02/15/11 22:42:04. Last, toGMTString converts a Date to the Greenwich Mean Time format like this:16 Feb 2011 06:42:04 GMT. To determine how far from GMT your local time zone is, use getTimezoneOffset, which returns the offset in minutes.

 

            Sample program:-

            class Date {
                         int dd, mm, yy;
                         public void initDate(){
                         dd = mm = yy = 0;
             }
             public void setDate(int d, int m, int y){
                         dd = d;
                        mm = m;
                        yy = y;
         



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)