A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Joined: Jun 03, 2005
Feb 02, 2009 10:07:37
I want to get month and year from the timestamp.
Ex: Timestamp timestamp = new Timestamp(new Date().getTime());
I am using timestamp.getMonth() and timestamp.getYear() to get month and date but both the methods are deprecated.
What methods should i use instead of these deprecated methods?
Joined: Aug 07, 2007
Feb 02, 2009 11:28:32
You should use a Calendar object.
You can obtain one for your locale, set it to the timestamp's time and use it as follows:
Calendar cal = Calendar.getInstance(); cal.setTime(myTimestamp); month = cal.get(Calendar.MONTH); year = cal.get(Calendar.YEAR);
Joined: Oct 27, 2005
Feb 02, 2009 12:32:21
And remember to compare the month against the month constants: Calendar.JANUARY - Calendar.DECEMBER. That way you won't get that confused when you notice that the months are 0-based
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
I agree. Here's the link:
subject: java Timestamp
Question on Gregorian Calendar
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015