Solved

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

Posted on 2005-04-19
371 Views
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
Question by:pokuri

LVL 92

Accepted Solution

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

LVL 15

Expert Comment

>> 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.

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

### Suggested Solutions

If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…