JAVA8出的新的时间日期API都是线程安全的,并且性能更好,代码更简洁!
LocalDate、LocalTime、LocalDateTime是新API里的基础对象,绝大多数操作都是围绕这几个对象来进行的,有必要搞清楚:
LocalDate : 只含年月日的日期对象
LocalTime :只含时分秒的时间对象
LocalDateTime : 同时含有年月日时分秒的日期对象
获取当前时间
LocalTime localTime = LocalTime.now();LocalDate localDate = LocalDate.now();LocalDateTime localDateTime = LocalDateTime.now();System.out.println("当前时间:" + localTime);System.out.println("当前日期:" + localDate);System.out.println("当前时间日期:" + localDateTime);
运行结果:
格式化日期
//格式化日期DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String now = localDateTime.format(formatter);System.out.println("格式化后当前时间日期:" + now);
运行结果:
解析时间
//解析时间LocalDateTime localDateTime = LocalDateTime.parse("-08-08 08:08:08", formatter);System.out.println("解析后时间:" + localDateTime);
运行结果:
除了基本的格式化,解析时间之外,LocalDateTime还提供了对时间的加减操作
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime localDateTime = LocalDateTime.now();System.out.println("当前时间日期:" + localDateTime.format(formatter));//增加一个月时间localDateTime = localDateTime.plusMonths(1);System.out.println("增加一个月时间后时间:" + localDateTime.format(formatter));//增加2周时间localDateTime = localDateTime.plusWeeks(2);System.out.println("增加2周时间后时间:" + localDateTime.format(formatter));//减少两天时间localDateTime = localDateTime.plusDays(-2);System.out.println("减少2天时间后时间:" + localDateTime.format(formatter));
运行结果:
除了使用plusMonths等方法外,还可以使用plus(long amountToAdd, TemporalUnit unit)方法
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");LocalDateTime localDateTime = LocalDateTime.now();System.out.println("当前时间日期:" + localDateTime.format(formatter));//增加一个月时间localDateTime = localDateTime.plus(1, ChronoUnit.MONTHS);System.out.println("增加一个月时间后时间:" + localDateTime.format(formatter));//增加2周时间localDateTime = localDateTime.plus(2, ChronoUnit.WEEKS);System.out.println("增加2周时间后时间:" + localDateTime.format(formatter));//减少两天时间localDateTime = localDateTime.plus(-2, ChronoUnit.DAYS);System.out.println("减少2天时间后时间:" + localDateTime.format(formatter));
运行结果:
JAVA8还提供了计算时间间隔,日期间隔的类
Duration:用于计算两个“时间”间隔
Period:用于计算两个“日期”间隔
LocalTime localTime1 = LocalTime.now();LocalTime localTime2 = LocalTime.of(18, 36, 33);System.out.println("时间1:" + localTime1);System.out.println("时间2:" + localTime2);Duration duration = Duration.between(localTime1, localTime2);System.out.println("两个时间之间相隔" + duration.toHours() + "小时");System.out.println("两个时间之间相隔" + duration.toMinutes() + "分钟");System.out.println("两个时间之间相隔" + duration.getSeconds() + "秒");
运行结果:
LocalDate localDate1 = LocalDate.of(, 2, 20);LocalDate localDate2 = LocalDate.of(, 5, 12);Period period = Period.between(localDate1, localDate2);System.out.println("日期1:" + localDate1);System.out.println("日期2:" + localDate2);System.out.println("两个日期之间相隔" + period.getYears() + "年" + period.getMonths() + "月" + period.getDays() + "日");
运行结果:
另外还提供了是否是闰年的方法
System.out.println("是否为闰年:" + localDate2.isLeapYear());
运行结果:
withDayOfYear(int dayOfYear)可以将日期设置为今年的多少天
LocalDateTime localDateTime3 = LocalDateTime.now();System.out.println("当前时间日期:" + localDateTime3.format(formatter));//将当前日期设置为今年的第50天localDateTime3 = localDateTime3.withDayOfYear(50);System.out.println("设置后日期:" + localDateTime3.format(formatter));
运行结果: