Java Time Zone When Parsing DateFormat

后端 未结 7 1684
梦谈多话
梦谈多话 2020-12-06 17:20

I had code that parses date as follows:

String ALT_DATE_TIME_FORMAT = \"yyyy-MM-dd\'T\'HH:mm:ss.SSSZ\";
SimpleDateFormat sdf = new SimpleDateFormat(
                 


        
7条回答
  •  没有蜡笔的小新
    2020-12-06 17:35

    Use JodaTime

    As a more concrete example to @Pangea's suggestion to use JodaTime, this is what you could use:

    String timestamp = "2012-09-17T04:11:46Z";
    
    DateTime date = ISODateTimeFormat.dateTimeParser().parseDateTime(timestamp);
    

    This correctly recognizes the UTC timezone. I haven't tried it with milliseconds in the string timestamp, but I'm confident it'll work just as well.

    Hope that helps others.

    JP

提交回复
热议问题