《Java8实战》读书笔记
已有的日期类Date、Calendar设计有缺陷
因此,Java8引入java.time提供了新的类,LocalDate、LocalTime、LocalDateTime、Instant、Duration、Period
//创建
LocalDate localDate = LocalDate.of(2023, 4, 1);
LocalDate parse = LocalDate.parse("2023-04-01");
LocalDate now = LocalDate.now();
//获取年、月、日 //后边的方法也一样的用
int year = now.getYear(); //now.get(ChronoField.YEAR)
int month = now.getMonth().getValue(); // now.getMonthValue(); //now.get(ChronoField.MONTH_OF_YEAR)
int day = now.getDayOfMonth(); // now.get(ChronoField.DAY_OF_MONTH);
System.out.println(now);
System.out.println(year);
System.out.println(month);
System.out.println(day);
//输出
2023-04-02
2023
4
2
//创建对象
LocalTime time1 = LocalTime.of(22, 3, 30);
LocalTime time2 = LocalTime.parse("22:03:30")
LocalTime time = LocalTime.now();
//获取时、分、秒 //后边的也一样的用
int hour = time.getHour(); //time.get(ChronoField.HOUR_OF_DAY)
int minute = time.getMinute(); //time.get(ChronoField.MINUTE_OF_HOUR);
int second = time.getSecond(); //time.get(ChronoField.SECOND_OF_MINUTE);
System.out.println(time);
System.out.println(hour);
System.out.println(minute);
System.out.println(second);
//输出
22:05:45.920
22
5
45
//创建
LocalDateTime d1 = LocalDateTime.of(2023, 4, 2, 22, 40, 30);
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
//组合日期和时间
LocalDateTime d2 = LocalDateTime.of(date, time);
//日期组合时间
LocalDateTime d3 = date.atTime(time);
LocalDateTime d4 = date.atTime(22, 40, 30);
//时间组合日期
LocalDateTime d5 = time.atDate(date);
//直接now
LocalDateTime now = LocalDateTime.now(); //2023-04-09T10:31:14.453
//获取日期 LocalDate
LocalDate date6 = now.toLocalDate(); //2023-04-09
//获取时间 LocalTime
LocalTime time6 = now.toLocalTime(); //10:31:14.453
如果你已经有一个LocalDate对象,想要创建它的一个修改版,最直接也最简单的方法是使用withAttribute方法。
withAttribute方法会创建对象的一个副本,并按照需要修改它的属性。
注意,下面的这段代码中所有的方法都返回一个修改了属性的对象。它们都不会修改原来的对象!
//2023-04-01
LocalDate date = LocalDate.of(2023, 4, 1);
//2018-04-01
LocalDate d1 = date.withYear(2018);
//2023-12-01
LocalDate d2 = date.withMonth(12);
//2023-04-20
LocalDate d3 = date.withDayOfMonth(20);
//2023-04-20
LocalDate d4 = date.with(ChronoField.DAY_OF_MONTH, 20);
//2023-04-01
LocalDate date = LocalDate.of(2023, 4, 1);
//2023-04-04
LocalDate d1 = date.plusDays(3);
//2021-04-01
LocalDate d2 = date.minusYears(2);
//2023-04-04
LocalDate d3 = date.plus(3, ChronoUnit.DAYS);
将日期调整到下个周日、下个工作日,或者是本月的最后一天。这时,你可以使用重载版本的with方法,向其传递一个提供了更多定制化选择的TemporalAdjuster对象,更加灵活地处理日期。对于最常见的用例,日期和时间API已经提供了大量预定义的TemporalAdjuster。你可以通过TemporalAdjuster类的静态工厂方法访问它
LocalDate date = LocalDate.of(2023, 4, 1);
//下周一 2023-04-03
LocalDate d1 = date.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));
//这个月最后一天2023-04-30
LocalDate d2 = date.with(TemporalAdjusters.lastDayOfMonth());
更多静态方法如下;
也可以自定义方法来实现TemporalAdjuster接口,实现自己的转换。
LocalDate now = LocalDate.of(2023,4,3);
//将日期转换成字符串
//20230403
String format1 = now.format(DateTimeFormatter.BASIC_ISO_DATE);
//2023-04-03
String format2 = now.format(DateTimeFormatter.ISO_LOCAL_DATE);
//将字符串转换成日期(用内置的静态实例)
LocalDate date1=LocalDate.parse("20140318", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate date2=LocalDate.parse("2014-03-18", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(date1);
System.out.println(date2);
//DateTimeFormatter.ofPattern(用指定字符串格式解析格式化)
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("dd MM/yyyy");
//18 03/2014
String formattedDate=date1.format(formatter);
LocalDate date3=LocalDate.parse(formattedDate, formatter);
//自定义DateTimeFormatter(通过DateTimeFormatterBuilder自己编程实现)
DateTimeFormatter italianFormatter=new DateTimeFormatterBuilder()
.appendText(ChronoField.DAY_OF_MONTH)
.appendLiteral(". ")
.appendText(ChronoField.MONTH_OF_YEAR)
.appendLiteral(" ")
.appendText(ChronoField.YEAR)
.parseCaseInsensitive()
.toFormatter(Locale.ITALIAN);
//1、LocalDate和Date互转
//1.1 LocalDate -> Date
LocalDate localDate = LocalDate.now();
Date date = Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
//1.2 Date -> LocalDate
Date date1 = new Date();
LocalDate localDate1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
//2.1 LocalDateTime -> Date
LocalDateTime localDateTime = LocalDateTime.now();
Date date2 = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
//2.2 date -> LocalDateTime
Date date3 = new Date();
LocalDateTime localDateTime1 = date3.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
还有一些涉及时区的类,没有记录;