Java8 日期时间API
2021-02-20 09:18
标签:日期时间 months data- val contain 结果 end mon local LocalDate LocalTime LocalDateTime Java8 日期时间API 标签:日期时间 months data- val contain 结果 end mon local 原文地址:https://www.cnblogs.com/coder-zyc/p/12682915.html
/**
* LocalDateTime相较于LocalDate,LocalTime,使用频率要高
*/
public void LocalDateTest() {
//now():获取当前的日期,时间,日期加时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
/**
* 打印结果:
* 2020-02-13
* 10:25:54.516
* 2020-02-13T10:25:54.516
*/
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
System.out.println("----------");
//of():设置指定的年,月,日,时,分,秒,没有偏移量
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 2, 12, 16, 30, 30);
/**
* 打印结果如下:
* 2020-02-12T16:30:30
*/
System.out.println(localDateTime1);
System.out.println("----------");
//getXxx():获取相关属性
/**
* 打印结果如下:
* 44
* 13
* THURSDAY
* FEBRUARY
* 2
* 39
*/
System.out.println(localDateTime.getDayOfYear());
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDate.getMonth().getValue());
System.out.println(localDateTime.getMinute());
System.out.println("----------");
//原始数据不可变性
//withXxx():设置相关属性
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
System.out.println("----------");
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println("localDateTime:" + localDateTime);
System.out.println("localDateTime2:" + localDateTime2);
//plusXxx:加操作
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println("localDateTime:" + localDateTime);
System.out.println("localDateTime3:" + localDateTime3);
System.out.println("----------");
//minusXxx:减操作
LocalDateTime localDateTime4 = localDateTime.minusMonths(3);
System.out.println("localDateTime:" + localDateTime);
System.out.println("localDateTime4:" + localDateTime4);
}
上一篇:c语言程序设计实验(四)
下一篇:C语言实验报告(四)