JDK 8中日期和时间的API
LocalDate、LocalTime、LocalDateTime
//今天是2021.12.27
@Test
public void testDate(){
//偏移量
Date date1 = new Date(2021 - 1900,12 - 1,27);
System.out.println(date1);//Mon Dec 27 00:00:00 CST 2021
}
/*
LocalDate、LocalTime、LocalDateTime 的使用
说明:
1.LocalDateTime相较于LocalDate、LocalTime,使用频率要高
2.类似于Calendar
*/
@Test
public void test1(){
//now():获取当前的日期、时间、日期+时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);//2021-12-27
System.out.println(localTime);//16:22:33.693
System.out.println(localDateTime);//2021-12-27T16:22:33.693
//of():设置指定的年、月、日、时、分、秒。没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 13, 23, 43);
System.out.println(localDateTime1);//2020-10-06T13:23:43
//getXxx():获取相关的属性
System.out.println(localDateTime.getDayOfMonth());//27
System.out.println(localDateTime.getDayOfWeek());//MONDAY
System.out.println(localDateTime.getMonth());//DECEMBER
System.out.println(localDateTime.getMonthValue());//12
System.out.println(localDateTime.getMinute());//22
//体现不可变性
//withXxx():设置相关的属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);//2021-12-27
System.out.println(localDate1);//2021-12-22
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);//2021-12-27T16:22:33.693
System.out.println(localDateTime2);//2021-12-27T04:22:33.693
//不可变性
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);//加3个月
System.out.println(localDateTime);//2021-12-27T16:22:33.693
System.out.println(localDateTime3);//2022-03-27T16:22:33.693
LocalDateTime localDateTime4 = localDateTime.minusDays(6);//减6天
System.out.println(localDateTime);//2021-12-27T16:22:33.693
System.out.println(localDateTime4);//2021-12-21T16:22:33.693
}
Instant
/*
Instant的使用
类似于 java.util.Date类
*/
@Test
public void test2(){
//now():获取本初子午线对应的标准时间
Instant instant = Instant.now();
System.out.println(instant);//2021-12-27T09:02:10.588Z
//添加时间的偏移量,得到东八区时间
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2021-12-27T17:02:10.588+08:00
//toEpochMilli():获取自1970年1月1日0时0分0秒(UTC)开始的毫秒数 ---> Date类的getTime()
long milli = instant.toEpochMilli();
System.out.println(milli);//1640595730588
//ofEpochMilli():通过给定的毫秒数,获取Instant实例 -->Date(long millis)
Instant instant1 = Instant.ofEpochMilli(1550475314878L);
System.out.println(instant1);//2019-02-18T07:35:14.878Z
}
DateTimeFormatter
重点掌握方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
/*
DateTimeFormatter:格式化或解析日期、时间
类似于SimpleDateFormat
*/
@Test
public void test3(){
// 方式一:预定义的标准格式。如:ISO_LOCAL_DATE_TIME;ISO_LOCAL_DATE;ISO_LOCAL_TIME
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期-->字符串
LocalDateTime localDateTime = LocalDateTime.now();
String str1 = formatter.format(localDateTime);
System.out.println(localDateTime);//2021-12-27T20:54:38.257
System.out.println(str1);//2021-12-27T20:54:38.257
//解析:字符串 -->日期
TemporalAccessor parse = formatter.parse("2019-02-18T15:42:18.797");
System.out.println(parse);//{},ISO resolved to 2019-02-18T15:42:18.797
// 方式二:
// 本地化相关的格式。如:ofLocalizedDateTime()
// FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT :适用于LocalDateTime
DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
//格式化
String str2 = formatter1.format(localDateTime);
System.out.println(str2);//2021年12月27日 下午08时54分38秒
// 本地化相关的格式。如:ofLocalizedDate()
// FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT : 适用于LocalDate
DateTimeFormatter formatter2 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
//格式化
String str3 = formatter2.format(LocalDate.now());
System.out.println(str3);//2021-12-27
// 重点: 方式三:自定义的格式。如:ofPattern(“yyyy-MM-dd hh:mm:ss”)
DateTimeFormatter formatter3 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
//格式化
String str4 = formatter3.format(LocalDateTime.now());
System.out.println(str4);//2021-12-27 08:54:38
//解析
TemporalAccessor accessor = formatter3.parse("2019-02-18 03:52:09");
System.out.println(accessor);
}