logo

目录

日期与时间(2)

ZonedDateTime

LocalDateTime总是表示本地日期和时间,要表示一个带时区的日期和时间,我们就需要ZonedDateTime
可以简单地把ZonedDateTime理解成LocalDateTimeZoneIdZoneIdjava.time引入的新的时区类,注意和旧的java.util.TimeZone区别。
要创建一个ZonedDateTime对象,有以下几种方法,一种是通过now()方法返回当前时间:
import java.time.*; public class Main { public static void main(String[] args) { ZonedDateTime zbj = ZonedDateTime.now(); // 默认时区 ZonedDateTime zny = ZonedDateTime.now(ZoneId.of("America/New_York")); // 用指定时区获取当前时间 System.out.println(zbj); System.out.println(zny); } }
本章目录
    1v1免费职业咨询