1. Instant
1.1. 概念
Instant
是 java.time
包中的一个类,它表示时间线上的一个瞬时点,默认(UTC)
表示从 1970-01-01T00:00:00Z
开始的秒数和纳秒数,不受时区变化或夏令时的影响。
1.2. 获取一个实例
# 获取当前时间(默认:UTC)
Instant instant = Instant.now();
# 获取当前时间(默认:UTC)
Instant instant = Instant.now(Clock.systemUTC());
# 根据给定的秒数和纳秒调整来创建 Instant
Instant instant = Instant.ofEpochSecond(10000,1000);
# 根据给定的毫秒数(从 1970-01-01T00:00:00Z 开始)创建 Instant
Instant instant = Instant.ofEpochMilli(10000);
# 将文本字符串解析成 Instant
Instant instant = Instant.parse("2023-01-01T00:00:00Z");
# 根据时区时间得到 Instant 实例
ZonedDateTime zonedDateTime = ZonedDateTime.now();
Instant instant = Instant.from(zonedDateTime);
1.3. Instant 转 LocalDateTime
Instant now = Instant.now();
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime zonedDateTime = now.atZone(zoneId);
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
1.4. LocalDateTime 转 Instant
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
LocalDateTime localDateTime = LocalDateTime.of(2024, 12, 30, 18, 29, 50);
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
Instant instant = zonedDateTime.toInstant();
2. LocalDateTime
2.1. 概念
表示 不包含时区
信息的一个日期时间类
2.2. 获取一个实例
LocalDateTime localDateTime1 = LocalDateTime.now();
LocalDateTime localDateTime2 = LocalDateTime.now(Clock.systemUTC());
LocalDateTime localDateTime3 = LocalDateTime.now(ZoneId.systemDefault());
LocalDateTime localDateTime4 = LocalDateTime.of(2024, 11, 01, 21, 50, 50);
LocalDateTime localDateTime5 = LocalDateTime.of(LocalDate.now(), LocalTime.now());
LocalDateTime localDateTime6 = LocalDateTime.ofEpochSecond(50000,50000, ZoneOffset.UTC);
LocalDateTime localDateTime7 = LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalDateTime localDateTime8 = LocalDateTime.from(zonedDateTime);
LocalDateTime localDateTime9 = LocalDateTime.parse("2023-01-01T00:00:00Z");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime localDateTime10 = LocalDateTime.parse("2023-01-01T00:00:00Z", formatter);
3. LocalDate
3.1. 概念
表示 不包含时区
信息的一个日期类
3.2. 获取一个实例
LocalDate localDate1 = LocalDate.now();
LocalDate localDate2 = LocalDate.now(Clock.systemUTC());
LocalDate localDate3 = LocalDate.now(ZoneId.systemDefault());
LocalDate localDate4 = LocalDate.of(2024, 12, 12);
LocalDate localDate5 = LocalDate.ofYearDay(2024, 11);
LocalDate localDate6 = LocalDate.ofEpochDay(50000);
ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalDate localDate7 = LocalDate.from(zonedDateTime);
LocalDate localDate8 = LocalDate.parse("2023-01-01T00:00:00Z");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate localDate9 = LocalDate.parse("2023-01-01T00:00:00Z", formatter);
4. LocalDate
4.1. 概念
表示 不包含时区
信息的一个时间类
4.2. 获取一个实例
LocalTime localTime1 = LocalTime.now();
LocalTime localTime2 = LocalTime.now(Clock.systemUTC());
LocalTime localTime3 = LocalTime.now(ZoneId.systemDefault());
LocalTime localTime4 = LocalTime.of(13, 40, 12);
LocalTime localTime5 = LocalTime.ofSecondOfDay(100);
LocalTime localTime6 = LocalTime.ofNanoOfDay(100);
ZonedDateTime zonedDateTime = ZonedDateTime.now();
LocalTime localTime7 = LocalTime.from(zonedDateTime);
LocalTime localTime8 = LocalTime.parse("2023-01-01T00:00:00Z");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalTime localTime9 = LocalTime.parse("2023-01-01T00:00:00Z", formatter);
5. DateTimeFormatter
5.1. 概念
DateTimeFormatter 类用于在日期、时间和字符串之间进行转换。
5.2. 实例化
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
DateTimeFormatter formatter3 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL);
DateTimeFormatter formatter4 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
DateTimeFormatter formatter5 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter formatter6 = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);