Search code examples
javadatesimpledateformatdate-formatdateformatter

What is the date format of 2019-06-17T20:27:23.706000000Z?


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.


Solution

  • 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);