目录
三、LocalDate、LocalTime、LocalDateTime的使用
一、Instant
@Test
public void test2(){
// now()获取本初子午线的时间
Instant now = Instant.now();
System.out.println(now);//2022-01-09T09:51:02.487524200Z
// 添加时间偏移量
OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
System.out.println(offsetDateTime);//2022-01-09T17:54:51.188335700+08:00 这是现在的时间
// toEpochMilli()获得时间戳的毫秒数
long l = now.toEpochMilli();
System.out.println(l);
// ofEpochMilli():根据时间戳获取Instant实例
Instant instant = Instant.ofEpochMilli(28288282l);//根据时间戳来创建时间,这是一个静态方法
System.out.println(instant);
}
二、DateTimeFormatter:格式化和解析
@Test
public void test3(){
// 方式一:预定义的标准格式:
DateTimeFormatter d = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
//格式化:日期--->字符串
LocalDateTime now = LocalDateTime.now();
String format = d.format(now);
System.out.println(format);
// 解析:字符串--->日期
TemporalAccessor parse = d.parse("2022-01-09T18:15:11.3300169");
System.out.println(parse);//{},ISO resolved to 2022-01-09T18:15:11.330016900
// 方式二:下面的每一个方法有三个常量参数,这是指格式不同,大抵没有区别
//本地化相关的格式。如: ofLocalizedDateTime()
//FormatStyle.LONG / FormatStyle.MEDIUM / FormatStyle.SHORT:适用于LocalDateTime
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
String format1 = dateTimeFormatter.format(now);
System.out.println(format1);//2022/1/9 下午6:26
// 本地化相关的格式。如: ofLocalizedDate()
// FormatStyle.FULL / FormatStyle.LONG / FormatStyle.MEDIUNM/FormatStyle.SHORT︰适用于LocalDate
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
String format2 = dateTimeFormatter1.format(LocalDate.now());
System.out.println(format2);//2022年1月9日星期日
// 重点:方式三:自定义的格式。如: ofPattern("yyyy-MM-dd hh :mm: ss E")
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh :mm: ss E");
// 格式化:
String format3 = formatter.format(LocalDateTime.now());
System.out.println(format3);//2022-01-09 06 :36: 09 周日
// 解析
TemporalAccessor parse1 = formatter.parse("2022-01-09 06 :36: 09 周日");
System.out.println(parse1);//{SecondOfMinute=9, MinuteOfHour=36, NanoOfSecond=0, MicroOfSecond=0, HourOfAmPm=6, MilliOfSecond=0},ISO resolved to 2022-01-09
}
三、LocalDate、LocalTime、LocalDateTime的使用
LocalDateTime的使用频率最高,三者的方法是一样的
@Test
public void test() {
// now():获取当前日期时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
// of()设置年月日时分秒没有偏移量
LocalDateTime time = LocalDateTime.of(2022, 1, 9, 17, 30, 1);
System.out.println(time);
// getXxx()
System.out.println(time.getDayOfMonth());
System.out.println(time.getDayOfYear());
// withXxx()设置相关的属性
LocalDateTime time1 = time.withDayOfMonth(30);//体现了不可变性
System.out.println(time);//没变
System.out.println(time1);//变了
// plusXxx() 在现有的基础上加上一个值
LocalDateTime time2 = time.plusHours(3);
System.out.println(time);
System.out.println(time2);
// minusXxx() 在现有的基础上减去一个值
LocalDateTime time3 = time.minusDays(10);
System.out.println(time);//2022-01-09T17:30:01
System.out.println(time3);//2021-12-30T17:30:01 居然可以直接回到2021年,还是很智能的
}