Convert timestamp to UTC timezone

后端 未结 2 1344
遥遥无期
遥遥无期 2021-01-03 08:14
public static long getCurrentEpochTimeStamp(String timeStamp) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy-MM-dd\'T\'HH:mm:ss\'.0Z\'\");         


        
相关标签:
2条回答
  • 2021-01-03 08:46

    Set time zone to your SimpleDateFormat object.

    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    
    0 讨论(0)
  • 2021-01-03 08:54

    Date is always in UTC and you cannot convert it. The problem is how to parse string representation. If timeStamp contains timezone, then pattern "yyyy-MM-dd'T'HH:mm:ss'.0Z'" is incorrect because it does not parse timezone. It should be "yyyy-MM-dd'T'HH:mm:ssZ" if timezone is in RFC 822 format, use X instead of Z if timezone is in ISO 8601. See SimpleDateFormat API

    0 讨论(0)
提交回复
热议问题