How to get local time of different time zones?

前端 未结 7 1169
青春惊慌失措
青春惊慌失措 2020-12-18 19:08

I want to get local time of different time zones using Java code. Based on the time zone passed to the function I need that time zone\'s local time. How to achieve this?

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 20:04

    In Java 8, you can use the ZonedDateTime.now(ZoneId zone) method:

    ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Tokyo"));
    LocalTime localTime = zonedDateTime.toLocalTime();
    
    System.out.println(localTime);
    

提交回复
热议问题