我正在处理需要将UTC时间戳转换为其他区域特定时间戳的要求。下面是不同时区的列表。将UTC时间戳转换为其他时区特定时间
CET UTC+0100
CST UTC+0800
CST UTC-0600
EST UTC-0500
IST UTC+0530
MET-1METDST
SGT UTC+0800
SST-8
public static String convertUTCTimeStampToOtherZoneTimestamp(Timestamp timestamp,String timeZone){
String zoneSpecificTimeStamp="";
try {
DateFormat gmtFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
TimeZone zoneTime = TimeZone.getTimeZone(timeZone);
gmtFormat.setTimeZone(zoneTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String inputDateString = sdf.format(timestamp);
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
zoneSpecificTimeStamp=gmtFormat.format(sdf.parse(inputDateString));
} catch (ParseException e) {
e.printStackTrace();
return zoneSpecificTimeStamp;
}
return zoneSpecificTimeStamp;
}
上面的代码是一样EST,PST,IST时区,但在列表中的程序给错误的结果中提到的其他时区做工精细。请更新我的正确解决方案
这已被问了很多次。请先搜索。提示:您应该使用时区名称,如“Europe/Paris”,而不是“CET”。 –
他其实是对的,同一地理位置在夏季和冬季有不同的时区。 – hoaz
如果您使用的是java 8,请尝试使用zoneddate时间https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html – Maverick