3

What is the date format of 2019-06-17T20:27:23.706000000Z? I need date format of this format of dates, also need to parse and convert it to java Date.

I tried with this:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

But, it's giving me incorrect results.

Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
Chinmay
  • 73
  • 3
  • 8
  • I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. Your format is ISO 8601. Use `Instant` or `OffsetDateTime`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Each of those will parse your string without any explicit formatter. – Ole V.V. Jun 24 '19 at 13:47
  • 1
    Hey, thank you for the info. Yes, i am using instant class which is way better than the legacy date-time classes – Chinmay Jun 24 '19 at 23:32

1 Answers1

2

Try this:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.text.DateFormat;
import java.util.TimeZone;

public class Main
{
    public static void main(String[] args){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
        try{
            Date date = sdf.parse("2019-06-17T20:27:23.706Z");
            System.out.println(date);
        }
        catch(Exception pe){
            System.out.println(pe);
        }

    }
}

output:

Mon Jun 17 20:27:23 UTC 2019

By the way, you can also try this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'");

Both gives the same result

Try using Instant from java.time package(java 8).

String value = "2019-06-17T20:27:23.706000000Z";
Instant instant = Instant.parse(value);
Date date = Date.from(instant);
Anubhav Singh
  • 8,321
  • 4
  • 25
  • 43
  • 1
    Thanks Anubav, if we use 2019-06-17T20:27:23.706000000Z it giving me result as Wed Jun 26 00:34:03 PDT 2019 which is incorrect.. so it can only support 3 digits in miliseconds? is there a way to handle more than 3 digits? – Chinmay Jun 23 '19 at 03:26
  • Yeah, because Milliseconds(SSS) can only be three digits – Anubhav Singh Jun 23 '19 at 03:27
  • check this: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Anubhav Singh Jun 23 '19 at 03:28
  • It seems to output strange/wrong results when we are adding more values after 3 digits – Anubhav Singh Jun 23 '19 at 03:29
  • So you should use only 3 digits. After that value will start to change. – Anubhav Singh Jun 23 '19 at 03:34
  • one quick question, the link u send https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html. I have gone through it, but i couldn't see anything mentioned like only 3 digits are accepted for milliseconds. Am i missing something? – Chinmay Jun 23 '19 at 04:59
  • 1
    Go through this SO answer and comments, I think you will find the answer. https://stackoverflow.com/questions/47926442/java-simpledateformat-pattern-parseexception/47926690 – Anubhav Singh Jun 23 '19 at 09:42
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 24 '19 at 13:42
  • 1
    Yeah, sure. I already included a solution from java.time package. – Anubhav Singh Jun 24 '19 at 13:45