• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Get Day,Month,Hours,Minute from a given Timestamp

Hi ,

     For example , the timestamp  is given as "2003-04-05-11.30.22.0000"

   How can I  retrieve the minutes,day,seconds from the given timestamp.


  I tried to retrieve it by using
   java.sql.Timestamp  actcmpStmp =  "2003-04-05-11.30.22.0000";

   int runday = actcmpStmp.getMinutes();
   
   But the methods used above are   DEPRECATED.
How can I retrieve seconds, minutes,day, etc from a given Timestamp without using Depracted Methods.
 Can anyone help me in this regard.

Thanks
ppp
0
pokuri
Asked:
pokuri
1 Solution
 
objectsCommented:
DateFormat sdf = new SimpleDateFormat("yyy-MM-dd-hh.mm.ss.SSSS");
Date d = sdf.parse("2003-04-05-11.30.22.0000");
Calendar cal = cal.getInstance();
cal.setTime(d);
int minutes = cal.get(Calendar.MINUTE);
int day = cal.get(Calendar.DAY_OF_MONTH);
int seconds = cal.get(Calendar.SECOND);


0
 
aozarovCommented:
>> DateFormat sdf = new SimpleDateFormat("yyy-MM-dd-hh.mm.ss.SSSS");
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-hh.mm.ss.SSSS"); //missed one y

Also, use H instead of h if this the hour is from 0  to 23 (as it seems so)
bare in mind that cal.get(Calendar.MONTH) returns a value from 0 to 11 (just add 1 to the returned value)
Note that SimpleDateFormat is not Thread safe.

For more info see:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
and
http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now