SimpleDateFormat get only the time

11,321

Your code is correct...

In the example time what you have given in the question(ie, "2012-04-19T23:05:00+0200") is missing MilliSeconds

Try passing this

getTime("2012-04-19T23:05:00.235+0200");

It should work.

Edit: As MH mentioned, If you dont want to use milliseconds you can change the code to

final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Share:
11,321
Jelmer Visser
Author by

Jelmer Visser

Updated on June 04, 2022

Comments

  • Jelmer Visser
    Jelmer Visser almost 2 years

    I have a problem when I try to get only the time from a Timestamp.

    An example of the Timestamp is:

    2012-04-19T23:05:00+0200

    I think the format is "yyyy-MM-dd'T'HH:mm:ss.SSSZ" right? And it must be "HH:mm".

    I use the following code, but it returnes nothing:

    public String getTime(String Vertrektijd){
            final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
            Date dateObj;
            String newDateStr = null;
            try
            {
                dateObj = df.parse(Vertrektijd);
                SimpleDateFormat fd = new SimpleDateFormat("HH:mm");
                newDateStr = fd.format(dateObj);
            }
            catch (Exception e)
            {
              e.printStackTrace();
            }
            return newDateStr;
        }
    

    Thanks for the help!