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

Posted on 2005-04-19
Last Modified: 2008-02-01
Hi ,

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

   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-";

   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.

Question by:pokuri
    LVL 92

    Accepted Solution

    DateFormat sdf = new SimpleDateFormat("");
    Date d = sdf.parse("2003-04-05-");
    Calendar cal = cal.getInstance();
    int minutes = cal.get(Calendar.MINUTE);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int seconds = cal.get(Calendar.SECOND);

    LVL 15

    Expert Comment

    >> DateFormat sdf = new SimpleDateFormat("");
    DateFormat sdf = new SimpleDateFormat(""); //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:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    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…
    Video by: Michael
    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…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now