您有任何问题或意见都能够在评论区回复哦,欢送大家一起来探讨,独特学习提高
新工夫类型 LocalDateTime,OffsetDateTime,LocalDate
// 截取到天,小时
System.out.println(LocalDateTime.now().truncatedTo(ChronoUnit.DAYS));
System.out.println(LocalDateTime.now().truncatedTo(ChronoUnit.HOURS));
// 输入
2020-05-06T00:00
2020-05-06T13:00
// 设置小时为 14 点
System.out.println(LocalDateTime.now().withHour(14));
// 减去 12 天
System.out.println(LocalDateTime.now().minusDays(12));
// 减少 12 小时
System.out.println(LocalDateTime.now().plusHours(12));
// 设定 0 时区工夫,截取到整日,设定 12 点
OffsetDateTime.now(ZoneOffset.of("+00:00")).truncatedTo(ChronoUnit.DAYS).withHour(12)
// 获取上海时区的工夫
OffsetDateTime.now(ZoneId.of("Asia/Shanghai"))
// 实间转换
LocalDate loc = LocalDate.parse("2020-01-02");
System.out.println(loc);
DateTimeFormatter usDateformatter = DateTimeFormatter.ofPattern("MM-dd-yyyy");
System.out.println(usDateformatter.format(loc));
// 输入
2020-01-02
01-02-2020
// 当 parse 的格局不是本地工夫格局是,须要通过对应的 formatter 格局
LocalDate newDate = LocalDate.parse("07-04-2020",usDateformatter);
System.out.println(newDate);
// 输入
2020-07-04
旧版本 time 类型 Calendar,Date
Calendar cal = Calendar.getInstance();
cal.set(2020,1,26);
Date date = cal.getTime();
System.out.println(date);
// 输入
Wed Feb 26 22:01:51 CST 2020
cal.add(Calendar.DAY_OF_MONTH,7);
System.out.println(cal.getTime());
// 输入
Wed Mar 04 22:01:51 CST 2020
新 Time 类型 Instant 是不可扭转的
System.out.println(Instant.MIN);
System.out.println(Instant.now());
System.out.println(Instant.MAX);
// 输入
-1000000000-01-01T00:00:00Z
2020-05-17T14:10:23.739Z
+1000000000-12-31T23:59:59.999999999Z
Duration,包含 toDays(),toHours(),toMinutes(),toNanos() 等办法,minus() 减去,plus 减少,multipliedBy 乘以
Instant start = Instant.now();
Thread.sleep(1);
Instant end = Instant.now();
Duration duration = Duration.between(start,end);
long millionSeconds = duration.toMillis();
System.out.println(millionSeconds);
// 输入
1
LocalDate
TemporalAdjusters 包含的办法:firstDayOfMonth,lastDayOfMonth,firstDayOfYear,lastDayOfYear,firstDayOfNextMonth,firstDayOfNextYear
firstInMonth(DayOfWeek.SUNDAY,lastInMonth(DayOfWeek.SUNDAY),dayOfWeekInMonth(2,DayOfWeek.SUNDAY)
next(DayOfWeek.SUNDAY),nextOrSame(DayOfWeek.SUNDAY),previous(DayOfWeek.SUNDAY),previousOrSame(DayOfWeek.SUNDAY)
LocalDate now = LocalDate.now();
LocalDate dateOfBirth = LocalDate.of(1987, Month.NOVEMBER, 23);
System.out.println(dateOfBirth);
System.out.println(now);
// 输入
1987-11-23
2020-05-18
Period p = dateOfBirth.until(now);
System.out.println("Years:"+p.getYears());
// 输入
Years:32
Long days = dateOfBirth.until(now,ChronoUnit.DAYS);
System.out.println("Days:"+days);
// 输入
Days:11865
// 其中的 month 差距只是余数,不是和下面的一样共计多少个月,而是除去整年后差的月数
System.out.println(p.get(ChronoUnit.YEARS));
System.out.println(p.get(ChronoUnit.MONTHS));
// 输入
32
5
LocalDate nextSunday = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
System.out.println(nextSunday);
// 输入
2020-05-24
LocalTime
LocalTime localTime = LocalTime.now();
System.out.println(localTime);
System.out.println(localTime.plusHours(8));
System.out.println(LocalTime.of(20,10));
// 输入
20:28:59.823
04:30:25.367
20:10
ZonedDateTime
Set<String> allZoneIds = ZoneId.getAvailableZoneIds();
for(String zoneId:allZoneIds){if(zoneId.contains("America")){System.out.println(zoneId);
}
}
// 输入很多临时疏忽
ZoneId ukTZ = ZoneId.of("Asia/Shanghai");
System.out.println(ukTZ);
ZonedDateTime currentMetting = ZonedDateTime.of(LocalDate.of(2020,Month.JUNE,18),
LocalTime.of(10,0),ZoneId.of("Asia/Shanghai"));
ZonedDateTime nextMetting = currentMetting.plus(Period.ofMonths(1));
ZonedDateTime nextUsMeeting = nextMetting.withZoneSameInstant(ZoneId.of("America/New_York"));
System.out.println(currentMetting);
System.out.println(nextMetting);
System.out.println(nextUsMeeting);
// 输入
2020-06-18T10:00+08:00[Asia/Shanghai]
2020-07-18T10:00+08:00[Asia/Shanghai]
2020-07-17T22:00-04:00[America/New_York]
DateTimeFormatter
System.out.println("ISO_DATE_TIME="+DateTimeFormatter.ISO_DATE_TIME.format(currentMetting));
System.out.println("RFC_1123_DATE_TIME="+DateTimeFormatter.RFC_1123_DATE_TIME.format(currentMetting));
// 输入
ISO_DATE_TIME=2020-06-18T10:00:00+08:00[Asia/Shanghai]
RFC_1123_DATE_TIME=Thu, 18 Jun 2020 10:00:00 +0800
老版本工夫和新版本工夫转换
Instant instant = Instant.now();
Date date = Date.from(instant);
instant = date.toInstant();
Timestamp timestamp = Timestamp.from(instant);
instant = timestamp.toInstant();