新的日期API:LocalDate
//of
LocalDate date = LocalDate.of(2022, 3, 18);
//parse
LocalDate date1 = LocalDate.parse("2022-03-18");
int year = date.getYear();
Month month = date.getMonth();
int day = date.getDayOfMonth();
DayOfWeek dow = date.getDayOfWeek();
int len = date.lengthOfMonth();
boolean leap = date.isLeapYear();
新的时间API:LocalTime
//of
LocalTime time = LocalTime.of(13, 45, 20);
//parse
LocalTime time1 = LocalTime.parse("13:45:20");
int hour = time.getHour();
int minute = time.getMinute();
int second = time.getSecond();
合并日期和时间API:LocalDateTime
LocalDate date = LocalDate.of(2022, 3, 18);
LocalTime time = LocalTime.of(13, 45, 20);
LocalDateTime dt1 = LocalDateTime.of(2022, Month.MARCH, 18, 13, 45, 20);
LocalDateTime dt2 = LocalDateTime.of(date, time);
LocalDateTime dt3 = date.atTime(13, 45, 20);
LocalDateTime dt4 = date.atTime(time);
LocalDateTime dt5 = time.atDate(date);
时间段API:Duration
LocalTime time1 = LocalTime.of(12, 30, 30);
LocalTime time2 = LocalTime.of(12, 40, 30);
Duration d1 = Duration.between(time1, time2);
System.out.println(d1.toMillis());
LocalDateTime dateTime1 = LocalDateTime.of(2022, 2, 12, 12, 30, 30);
LocalDateTime dateTime2 = LocalDateTime.of(2022, 2, 12, 12, 40, 30);
Duration d2 = Duration.between(dateTime1, dateTime2);
System.out.println(d2.getSeconds());
//十分钟后
Duration tenMinutes = Duration.ofMinutes(10);
Duration tenMinutes1 = Duration.of(10, ChronoUnit.MINUTES);
时间段API:Period
//计算两个日期间隔天数
Period period = Period.between(LocalDate.of(2022, 3, 12), LocalDate.of(2022, 3, 22));
System.out.println(period.getDays());
//十天之后
Period tenDays = Period.ofDays(10);
//三周之后
Period threeWeeks = Period.ofWeeks(3);
//两年六个月零1天后
Period twoYearsSixMonthsOneDay = Period.of(2, 6, 1);
操纵日期
//以比较直观的方式操纵LocalDate的属性
LocalDate date1 = LocalDate.of(2022, 3, 18);
//修改年份
LocalDate date2 = date1.withYear(2021);
//修改天
LocalDate date3 = date2.withDayOfMonth(25);
//修改月份
LocalDate date4 = date3.with(ChronoField.MONTH_OF_YEAR, 9);
//以相对方式修改LocalDate对象的属性
LocalDate d1 = LocalDate.of(2022, 3, 18);
//增加一周
LocalDate d2 = date1.plusWeeks(1);
//减去3年
LocalDate d3 = date2.minusYears(3);
//增加三个月
LocalDate d4 = date3.plus(6, ChronoUnit.MONTHS);
使用 TemporalAdjuster修改日期
LocalDate date1 = LocalDate.of(2022, 3, 18);
//获取下一个星期天
LocalDate date2 = date1.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
//获取这个月的最后一天
LocalDate date3 = date2.with(TemporalAdjusters.lastDayOfMonth());
格式化
LocalDate date = LocalDate.of(2022, 3, 18);
String s1 = date.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = date.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(s1);
System.out.println(s2);
//按照某个模式创建DateTimeFormatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate date1 = LocalDate.of(2022, 3, 18);
String formattedDate = date1.format(formatter);
LocalDate date2 = LocalDate.parse(formattedDate, formatter);
System.out.println(formattedDate);
//创建一个本地化的DateTimeFormatter
DateTimeFormatter italianFormatter =
DateTimeFormatter.ofPattern("d. MMMM yyyy", Locale.ITALIAN);
LocalDate date3 = LocalDate.of(2022, 3, 18);
String formattedDate1 = date3.format(italianFormatter);
System.out.println(formattedDate1);
// 构造一个DateTimeFormatter
DateTimeFormatter dateTimeFormatter = new DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_MONTH)
.appendLiteral(". ")
.appendText(ChronoField.MONTH_OF_YEAR)
.appendLiteral(" ")
.appendText(ChronoField.YEAR)
.parseCaseInsensitive()
.toFormatter(Locale.ITALIAN);
时区
ZoneId romeZone = ZoneId.of("Europe/Rome");
ZoneId zoneId = TimeZone.getDefault().toZoneId();
//为时间点添加时区信息
LocalDate date = LocalDate.of(2022, Month.MARCH, 18);
ZonedDateTime zdt1 = date.atStartOfDay(romeZone);
LocalDateTime dateTime = LocalDateTime.of(2022, Month.MARCH, 18, 13, 45);
ZonedDateTime zdt2 = dateTime.atZone(romeZone);
Instant instant = Instant.now();
ZonedDateTime zdt3 = instant.atZone(romeZone);