What is this date format? 2011-08-12T20:17:46.384Z

前端 未结 8 2153
日久生厌
日久生厌 2020-11-22 16:56

I have the following date: 2011-08-12T20:17:46.384Z. What format is this? I\'m trying to parse it with Java 1.4 via DateFormat.getDateInstance().parse(

8条回答
  •  一生所求
    2020-11-22 17:44

    There are other ways to parse it rather than the first answer. To parse it:

    (1) If you want to grab information about date and time, you can parse it to a ZonedDatetime(since Java 8) or Date(old) object:

    // ZonedDateTime's default format requires a zone ID(like [Australia/Sydney]) in the end.
    // Here, we provide a format which can parse the string correctly.
    DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
    ZonedDateTime zdt = ZonedDateTime.parse("2011-08-12T20:17:46.384Z", dtf);
    

    or

    // 'T' is a literal.
    // 'X' is ISO Zone Offset[like +01, -08]; For UTC, it is interpreted as 'Z'(Zero) literal.
    String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
    
    // since no built-in format, we provides pattern directly.
    DateFormat df = new SimpleDateFormat(pattern);
    
    Date myDate = df.parse("2011-08-12T20:17:46.384Z");
    

    (2) If you don't care the date and time and just want to treat the information as a moment in nanoseconds, then you can use Instant:

    // The ISO format without zone ID is Instant's default.
    // There is no need to pass any format.
    Instant ins = Instant.parse("2011-08-12T20:17:46.384Z");
    

提交回复
热议问题