public static String getTimeByZoneID(String zoneID) {
String formatted = null;
if (ZoneId.getAvailableZoneIds().contains(zoneID)) {
ZoneId zoneId = ZoneId.of(zoneID);
ZonedDateTime now = ZonedDateTime.now(zoneId);
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss 'UTC'xxx");
formatted = now.format(dateTimeFormatter);
} else {
System.out.println("zoneID有误!");
}
return formatted;
}
public static void main(String[] args) {
String timeZoneByID = getTimeByZoneID("America/New_York"); // Asia/Shanghai
System.out.println(timeZoneByID);
}
标签:getTimeByZoneID,now,String,根据,当地,formatted,zoneID,获取,ZoneId
From: https://www.cnblogs.com/ritchieojo/p/18278177