JDK8时间相关类
JDK8时间类类名 | 作用 |
---|
ZoneId | 时区 |
Instant | 时间戳 |
ZoneDateTime | 带时区的时间 |
DateTimeFormatter | 用于时间的格式化和解析 |
LocalDate | 年、月、日 |
LocalTime | 时、分、秒 |
LocalDateTime | 年、月、日、时、分、秒 |
Duration | 时间间隔(秒,纳,秒) |
Period | 时间间隔(年,月,日) |
ChronoUnit | 时间间隔(所有单位) |
4.1 ZoneId 时区
Set<String> zoneIds = ZoneId.getAvailableZoneIds();
System.out.println(zoneIds.size());
System.out.println(zoneIds);
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
ZoneId zoneId1 = ZoneId.of("Asia/Pontianak");
System.out.println(zoneId1);
4.2 Instant 时间戳
Instant now = Instant.now();
System.out.println(now);
Instant instant1 = Instant.ofEpochMilli(0L);
System.out.println(instant1);
Instant instant2 = Instant.ofEpochSecond(1L);
System.out.println(instant2);
Instant instant3 = Instant.ofEpochSecond(1L, 1000000000L);
System.out.println(instant3);
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time);
Instant instant4=Instant.ofEpochMilli(0L);
Instant instant5 =Instant.ofEpochMilli(1000L);
boolean result1=instant4.isBefore(instant5);
System.out.println(result1);
boolean result2 = instant4.isAfter(instant5);
System.out.println(result2);
Instant instant6 =Instant.ofEpochMilli(3000L);
System.out.println(instant6);
Instant instant7 =instant6.minusSeconds(1);
System.out.println(instant7);
4.3 ZoneDateTime 带时区的时间
ZonedDateTime now = ZonedDateTime.now();
System.out.println(now);
ZonedDateTime time1 = ZonedDateTime.of(2023, 10, 1,
11, 12, 12, 0, ZoneId.of("Asia/Shanghai"));
System.out.println(time1);
Instant instant = Instant.ofEpochMilli(0L);
ZoneId zoneId = ZoneId.of("Asia/Shanghai");
ZonedDateTime time2 = ZonedDateTime.ofInstant(instant, zoneId);
System.out.println(time2);
ZonedDateTime time3 = time2.withYear(2000);
System.out.println(time3);
ZonedDateTime time4 = time3.minusYears(1);
System.out.println(time4);
ZonedDateTime time5 = time4.plusYears(1);
System.out.println(time5);
4.4DateTimeFormatter 用于时间的格式化和解析
ZonedDateTime time = Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
DateTimeFormatter dtf1=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm;ss EE a");
System.out.println(dtf1.format(time));
4.5LocalDate 年、月、日
LocalDate nowDate = LocalDate.now();
LocalDate ldDate = LocalDate.of(2023, 1, 1);
System.out.println("指定日期:" + ldDate);
System.out.println("=============================");
int year = ldDate.getYear();
System.out.println("year: " + year);
Month m = ldDate.getMonth();
System.out.println(m);
System.out.println(m.getValue());
int month = ldDate.getMonthValue();
System.out.println("month: " + month);
int day = ldDate.getDayOfMonth();
System.out.println("day:" + day);
int dayofYear = ldDate.getDayOfYear();
System.out.println("dayOfYear:" + dayofYear);
DayOfWeek dayOfWeek = ldDate.getDayOfWeek();
System.out.println(dayOfWeek);
System.out.println(dayOfWeek.getValue());
System.out.println(ldDate.isBefore(ldDate));
System.out.println(ldDate.isAfter(ldDate));
LocalDate withLocalDate = ldDate.withYear(2000);
System.out.println(withLocalDate);
LocalDate minusLocalDate = ldDate.minusYears(1);
System.out.println(minusLocalDate);
LocalDate plusLocalDate = ldDate.plusDays(1);
System.out.println(plusLocalDate);
LocalDate birDate = LocalDate.of(2000, 1, 1);
LocalDate nowDate1 = LocalDate.now();
MonthDay birMd = MonthDay.of(birDate.getMonthValue(), birDate.getDayOfMonth());
MonthDay nowMd = MonthDay.from(nowDate1);
System.out.println("今天是你的生日吗? " + birMd.equals(nowMd));
4.6 LocalTime 时、分、秒
LocalTime nowTime = LocalTime.now();
System.out.println("今天的时间:" + nowTime);
int hour = nowTime.getHour();
System.out.println("hour: " + hour);
int minute = nowTime.getMinute();
System.out.println("minute: " + minute);
int second = nowTime.getSecond();
System.out.println("second:" + second);
int nano = nowTime.getNano();
System.out.println("nano:" + nano);
System.out.println("------------------------------------");
System.out.println(LocalTime.of(8, 20));
System.out.println(LocalTime.of(8, 20, 30));
System.out.println(LocalTime.of(8, 20, 30, 150));
LocalTime mTime = LocalTime.of(8, 20, 30, 150);
System.out.println(nowTime.isBefore(mTime));
System.out.println(nowTime.isAfter(mTime));
System.out.println(nowTime.withHour(10));
System.out.println(nowTime.plusHours(10));
4.7 LocalDateTime 年、月、日、时、分、秒
LocalDateTime nowDateTime = LocalDateTime.now();
System.out.println("今天是:" + nowDateTime);
System.out.println(nowDateTime.getYear());
System.out.println(nowDateTime.getMonthValue());
System.out.println(nowDateTime.getDayOfMonth());
System.out.println(nowDateTime.getHour());
System.out.println(nowDateTime.getMinute());
System.out.println(nowDateTime.getSecond());
System.out.println(nowDateTime.getNano());
System.out.println("dayofYear:" + nowDateTime.getDayOfYear());
System.out.println(nowDateTime.getDayOfWeek());
System.out.println(nowDateTime.getDayOfWeek().getValue());
System.out.println(nowDateTime.getMonth());
System.out.println(nowDateTime.getMonth().getValue());
LocalDate ld = nowDateTime.toLocalDate();
System.out.println(ld);
LocalTime lt = nowDateTime.toLocalTime();
System.out.println(lt.getHour());
System.out.println(lt.getMinute());
System.out.println(lt.getSecond());
4.8 Duration 时间间隔(秒,纳,秒)
- 可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数,支持
LocalTime、LocalDateTime、Instant等时间
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1, 0, 0, 0);
System.out.println(birthDate);
Duration duration = Duration.between(birthDate, today);
System.out.println("相差的时间间隔对象:" + duration);
System.out.println("============================================");
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toMillis());
System.out.println(duration.toNanos());
4.9 Period 时间间隔(年,月,日)
- 可以用于计算两个 LocalDate对象 相差的年数、月数、天数
LocalDate today = LocalDate.now();
System.out.println(today);
LocalDate birthDate = LocalDate.of(2000, 1, 1);
System.out.println(birthDate);
Period period = Period.between(birthDate, today);
System.out.println("相差的时间间隔对象:" + period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
System.out.println(period.toTotalMonths());
4.10 ChronoUnit 时间间隔(所有单位)
LocalDateTime today = LocalDateTime.now();
System.out.println(today);
LocalDateTime birthDate = LocalDateTime.of(2000, 1, 1,0, 0, 0);
System.out.println(birthDate);
System.out.println("相差的年数:" + ChronoUnit.YEARS.between(birthDate, today));
System.out.println("相差的月数:" + ChronoUnit.MONTHS.between(birthDate, today));
System.out.println("相差的周数:" + ChronoUnit.WEEKS.between(birthDate, today));
System.out.println("相差的天数:" + ChronoUnit.DAYS.between(birthDate, today));
System.out.println("相差的时数:" + ChronoUnit.HOURS.between(birthDate, today));
System.out.println("相差的分数:" + ChronoUnit.MINUTES.between(birthDate, today));
System.out.println("相差的秒数:" + ChronoUnit.SECONDS.between(birthDate, today));
System.out.println("相差的毫秒数:" + ChronoUnit.MILLIS.between(birthDate, today));
System.out.println("相差的微秒数:" + ChronoUnit.MICROS.between(birthDate, today));
System.out.println("相差的纳秒数:" + ChronoUnit.NANOS.between(birthDate, today));
System.out.println("相差的半天数:" + ChronoUnit.HALF_DAYS.between(birthDate, today));
System.out.println("相差的十年数:" + ChronoUnit.DECADES.between(birthDate, today));
System.out.println("相差的世纪(百年)数:" + ChronoUnit.CENTURIES.between(birthDate, today));
System.out.println("相差的千年数:" + ChronoUnit.MILLENNIA.between(birthDate, today));
System.out.println("相差的纪元数:" + ChronoUnit.ERAS.between(birthDate, today));