Java8 日期时间API
Posted coder-zyc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java8 日期时间API相关的知识,希望对你有一定的参考价值。
-
LocalDate
-
LocalTime
-
/** * 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); }
以上是关于Java8 日期时间API的主要内容,如果未能解决你的问题,请参考以下文章