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?

keeru
  • 87
  • 6
  • 1
    What time is `24:52:38`? Are you using a [30-hour clock](https://en.wikipedia.org/wiki/Date_and_time_notation_in_Japan#Time)? – Sweeper May 09 '20 at 14:57
  • In your last sentence in the question above, I think you mean 'UTC to EDT' which differ by 4 hours. – kayvis May 09 '20 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](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 10 '20 at 21:39

1 Answers1

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
kayvis
  • 563
  • 2
  • 9