根据时区获取当地时间

时区列表

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);
}
posted @ 2024-07-01 15:35  多多鱼^._.^  阅读(47)  评论(0)    收藏  举报