java.text.Parse Exception : Unparseable Date

前端 未结 2 1430
旧时难觅i
旧时难觅i 2021-01-18 16:01

I have the following code:

  String ModifiedDate = \"1993-06-08T18:27:02.000Z\" ;  
  SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-         


        
相关标签:
2条回答
  • 2021-01-18 16:27

    The Z pattern latter indicates an RFC 822 time zone. Your string

    String ModifiedDate = "1993-06-08T18:27:02.000Z" ;  
    

    does not contain such a time zone. It contains a Z literally.

    You'll want a date pattern, that similarly to the literal T, has a literal Z.

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

    If you meant for Z to indicate Zulu time, add that as a timezone when constructing the SimpleDateFormat

    sdf.setTimeZone(TimeZone.getTimeZone("Zulu"));;
    
    0 讨论(0)
  • 2021-01-18 16:32

    The answer by Sotirios Delimanolis is correct. The Z means Zulu time, a zero offset from UTC (+00:00). In other words, not adjusted to any time zone.

    Joda-Time

    FYI, the Joda-Time library make this work much easier, as does the new java.time package in Java 8.

    The format you are using is defined by the ISO 8601 standard. Joda-Time and java.time both parse & generate ISO 8601 strings by default.

    A DateTime in Joda-Time knows its own assigned time zone. So as part of the process of parsing, specify a time zone to adjust.

    DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" );
    DateTime dateTime = new DateTime( "1993-06-08T18:27:02.000Z", timeZone );
    String output = dateTime.toString();
    

    You can keep the DateTime object in Universal Time if desired.

    DateTime dateTime = new DateTime( "1993-06-08T18:27:02.000Z", DateTimeZone.UTC );
    

    When required by other classes, you can generate a java.util.Date object.

    java.util.Date date = dateTime.toDate();
    
    0 讨论(0)
提交回复
热议问题