0

I am trying to convert "2021-05-14T13:42:48.000Z" string to Date Object.

I have tried this:

DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DDHH:MM:SS");

And also this, which i saw on stackoverflow only:-

DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD'T'HH:MM:SS'A'");

But none of it worked.

How can i convert this string to my date object?

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Sagar Balyan
  • 620
  • 6
  • 20

3 Answers3

1

You have used the date-time format incorrectly. It's important to note that the date-time formats have different meanings between capitalized and small letters.

For example: Capital MM means months, whereas small mm means minutes.

To know more about the date formats, you can refer this: https://cheatography.com/pezmat/cheat-sheets/date-time-formats/pdf/

or this: https://devhints.io/datetime

And the answer for your case is:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
Maulik Hirani
  • 1,673
  • 1
  • 12
  • 19
  • While this will parse the date string without error, you'll get a date with the wrong timezone. Z indicates a UTC time, and this format string will return a date in your current timezone. – CSmith May 14 '21 at 11:03
1

Assuming your date string always represents a UTC time (with the 'Z'), you can use format string:

yyyy-MM-dd'T'HH:mm:ss.SSSZ

but you'll first need to replace the Z in your date string with the fixed timezone "+0000", as in "2021-05-14T13:42:48.000+0000".

Try this:

String myDateString = "2021-05-14T13:42:48.000Z"
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
Date myDate = dateFormat.parse (myDateString.replace("Z","+0000"));

This will return a date correctly adjusted for your current timezone, in my case 9:42:48am EDT.

There is a more detailed discussion at Converting ISO 8601-compliant String to java.util.Date which you may find useful.

CSmith
  • 13,318
  • 3
  • 39
  • 42
1

Please do not use SimpleDateFormat or even java.date. All these classes are deprecated.

Instead, rely on the Android available java.time package.

In short:

val source = "2021-05-14T13:42:48.000Z"
val parsed = ZonedDateTime.parse(source)

This will correctly parse the timezone (Z for Zulu/UTC/GMT).

You can verify this, by simply converting the parsed Zoned date time into, for example, Europe/Amsterdam time (which is +2).

val source = "2021-05-14T13:42:48.000Z"
val parsed = ZonedDateTime.parse(source)

parsed.toString() // prints: 2021-05-14T13:42:48Z
parsed.zone // prints: "Z" 
ZoneId.of(parsed.zone.id) // returns the ZoneOffset "Z" (correct)

// Convert to Amsterdam Time

val amsterdamDateTime = parsed.withZoneSameInstant(ZoneId.of("Europe/Amsterdam"))

amsterdamDateTime.toString() // prints: 2021-05-14T15:42:48+02:00[Europe/Amsterdam] (2 hours ahead of the Zulu time, also correct).

parsed.format(DateTimeFormatter.ISO_DATE_TIME).toString() // Prints: 2021-05-14T13:42:48Z (correct)

So as you can see, these classes do the right thing (most of the time).

I suggest you use them.

Martin Marconcini
  • 26,875
  • 19
  • 106
  • 144