我获得到的都是格林威治时间,我就是想获取本地的北京时间。我要怎么获取?各位上个demo给我看看。我的代码是
还有一个问题,就是怎么把上面的 time 转换成 Date 类型?
我获得到的都是格林威治时间,我就是想获取本地的北京时间。我要怎么获取?各位上个demo给我看看。我的代码是
还有一个问题,就是怎么把上面的 time 转换成 Date 类型?
public static void main(String[] args) throws Exception {
// 当前系统时区
ZoneId currentZone = OffsetDateTime.now().getOffset();
// 新时区,北京时间对应的时区是“GMT+8”
ZoneId newZone = ZoneId.of("GMT+8");
// 时区转换
LocalDateTime localDateTime = LocalDateTime.now().atZone(currentZone).withZoneSameInstant(newZone).toLocalDateTime();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(localDateTime.format(dtf));
}