简体   繁体   中英

Convert timestamp to UTC timezone

public static long getCurrentEpochTimeStamp(String timeStamp) throws Exception {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'.0Z'");
    Date date = sdf.parse(timeStamp);
    return date.getTime();
}

This method returns epoch current timestamp, I need to convert this to UTC timezone.

将时区设置为SimpleDateFormat对象。

sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM