The Perfect Date

I thought I should blog this so that no one else gets hurt  🙂

Sorry to disappoint, I’m talking about java.util.Date and not ‘dating’ in general.

We all know how to use the Date class…

Date date = new Date();

Now ‘date’ represents the current date …Try printing it and you will have something like this ….Thu Mar 17 20:37:14 IST 2011

 

Of course we know that the Date class has overridden the equals method.. And also know that the following should NOT be done…

someMethod(){

Date date1 = new Date();

//lots of code here

//some more code here

Date date2 = new Date(); //date2 might be created minutes later after date1

S.O.P(date1.equals(date2)); //No surprises, will result false

}

Where am I getting at…

Let’s say you wanted a Date object for 22nd Nov 2009 (it’s my favorite date). How do we get it? Ans: java.util.Calendar

FYI,  the Date class now has just two (non-deprecated) constructors…

 

Date()
Allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.

Date(long date)
Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as “the epoch”, namely January 1, 1970, 00:00:00 GMT.

So Here comes the ‘Calendar’ to the rescue…

 

A simple Calendar cal = Calendar.getInstance();    //gives you calendar object representing current date

Don’t worry, Calendar class has lots of methods to set the desired date and time

 

1> void set(int field, int value)
Sets the given calendar field to the given value.

 

2> void set(int year, int month, int date)
Sets the values for the calendar fields YEAR, MONTH, and DAY_OF_MONTH.

 

3> void set(int year, int month, int date, int hourOfDay, int minute)
Sets the values for the calendar fields YEAR, MONTH, DAY_OF_MONTH, HOUR_OF_DAY, and MINUTE.

 

4> void set(int year, int month, int date, int hourOfDay, int minute, int second)
Sets the values for the fields YEAR, MONTH, DAY_OF_MONTH, HOUR, MINUTE, and SECOND.

So let’s make use of the Calendar class to get the desired date object.

Date getMyFavDate(){

Calendar cal = Calendar.getInstance();

cal.set(2009, 11, 22);

return cal.getTime(); //Returns Date

}

Now what happens when I do this…

someMethod(){

Date date1 = getMyFavDate();

//lots of code here

//some more code here

Date date2 = getMyFavDate();

S.O.P(date1.equals(date2));

}

This again would return false, as date1 and date2 have different time attributes.

So lets modify getMyfavDate() ..

Date getMyFavDate(){

Calendar cal = Calendar.getInstance();

cal.set(2009, 11, 22, 0, 0, 0); //This time we have taken care of ‘time’…or have we?

return cal.getTime(); //Returns Date

}

Well, sadly, S.O.P(date1.equals(date2)) would still result as false as the Milli seconds do not match.

 

The set methods of Calendar (earlier mentioned as point 2, 3, 4) are all convenience methods of setting date and time…internally they all invoke the following

1> void set(int field, int value)
Sets the given calendar field to the given value.

So we need to add the missing code line

Date getMyFavDate(){

Calendar cal = Calendar.getInstance();

cal.set(2009, 11, 22, 0, 0, 0);

cal.set(Calendar.MILLISECOND, 0);

return cal.getTime(); //Returns Date

}

Now we would get S.O.P(date1.equals(date2)) as TRUE 🙂

All this can greatly be avoided when you use any DateUtils Class … For example org.apache.commons.lang.time.DateUtils, It provides utility methods like

public static boolean isSameDay(Date date1, Date date2)

and

public static boolean isSameInstant(Date date1, Date date2)

This clearly distinguishes which one would consider time when comparing dates.

But in case you are not comfortable using the Util classes, do make you of the Calendar class in the following way.

final DateFormat df = new SimpleDateFormat(“dd-MMM-yyyy”);

final Date myBirthDay = df.parse(“28-FEB-1984”);

Now if you want to check if today is my birthday or not…

checkIfItsMyBirthDay(){

Calendar todayCal = Calendar.getInstance();

Calendar dobCal = Calendar.getInstance();

dobCal.setTime(myBirthDay);

if((todayCal.get(Calendar.MONTH) == dobCal.get(Calendar.MONTH) &&

(todayCal.get(Calendar.DAY_OF_MONTH) == dobCal. get(Calendar.DAY_OF_MONTH))){

S.O.P(“Happy  BirthDay!!!”)

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s