0

i am trying to convert date and timestamp using .

public static void main(String args[]) {
list.add("2020-04-06T00:52:38+0000");
        list.add("2020-04-06T24:52:38+0000");
        list.add("2020-04-06T12:52:38+0000");

        Date createdTime = null;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");
        try {
             for (i =0; i < list.size(); i++) {
                    createdTime = df.parse(list.get(i));
                    System.out.print(i + " : "  + "Media Created_on : " + createdTime + "\n"); 
             }
        } catch (ParseException e) {
            e.printStackTrace();
        }
}

Output :

0 : Media Created_on : Sun Apr 05 20:52:38 EDT 2020
1 : Media Created_on : Mon Apr 06 20:52:38 EDT 2020
2 : Media Created_on : Sun Apr 05 20:52:38 EDT 2020

Here it is converted from IST to EDT which is 4 hrs ahead. So for the last iteration, why it is 13 hrs difference for 12 hours?

3
  • 1
    What time is 24:52:38? Are you using a 30-hour clock? Commented May 9, 2020 at 14:57
  • In your last sentence in the question above, I think you mean 'UTC to EDT' which differ by 4 hours. Commented May 9, 2020 at 14:59
  • 1
    I recommend you don’t use Date, DateFormat and SimpleDateFormat. Those classes are poorly designed and long outdated, the last two in particular notoriously troublesome. Instead use OffsetDateTime and DateTimeFormatter, both from java.time, the modern Java date and time API. Commented May 10, 2020 at 21:39

1 Answer 1

0

There's an error in your formatter declaration.

What you've configured:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ssZ");

What it should be, to meet your expected result:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

Notice that the hour should have been HH instead of hh

Then the result would be:

0 : Media Created_on : Sun Apr 05 20:52:38 EDT 2020
1 : Media Created_on : Mon Apr 06 20:52:38 EDT 2020
2 : Media Created_on : Sun Apr 06 08:52:38 EDT 2020
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.