Getting the day, month, and year from a date object

Now that convenient methods such as Date.getDay(), Date.getMonth(), and Date.getYear() are inconveniently deprecated, here’s the undeprecated way to get the day, month and year from a date object.

GregorianCalendar cal = new GregorianCalendar();
cal.setTime(someDate);
int day = cal.get(Calendar.DAY_OF_WEEK);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);

Note that the months are zero-based (January is 0).

Advertisements

About buffalobillion

Web Developer, JavaScript Balrog, Java dude, Ruby/Rails enthusiast. Guitar Playa.
This entry was posted in Uncategorized. Bookmark the permalink.

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