统一使用java.time.*包下的类
1、获取当前的日期、时间、日期加时间
LocalDate todayDate = LocalDate.now(); //今天的日期 LocalTime now = LocalTime.now(); //此刻的时间--精确到毫秒 LocalDateTime now = LocalDateTime.now(); //此刻时间--年-月-日-时-分-秒-毫秒
2、获取单独年日月时分秒
LocalDateTime now = LocalDateTime.now(); System.out.println("年:"+now.getYear()); System.out.println("月:"+now.getMonthValue()); System.out.println("日:"+now.getDayOfMonth()); System.out.println("时:"+now.getHour()); System.out.println("分:"+now.getMinute()); System.out.println("秒:"+now.getSecond()); System.out.println("该日期是该年的第"+now.getDayOfYear()+"天");
3、将特定的时间转成LocalDate、LocalTime、LocalDateTime 都有很多的重载的方法
LocalDate of1 = LocalDate.of(2019, 1, 16); System.out.println(of1); LocalTime of2 = LocalTime.of(9, 25, 36); System.out.println(of2); LocalDateTime of3 = LocalDateTime.of(of1, of2); System.out.println(of3); LocalDateTime of4 = LocalDateTime.of(2019, 1, 14, 9, 26, 25); System.out.println(of4);
4、判断时间是否相等(日期、时间、日期加时间)时间的比较精确到纳秒,可以先将时间进行格式化,然后比较,可以控制精度问题
// 日期 LocalDate date1 = LocalDate.of(2019, 1, 17); LocalDate date2 = LocalDate.now(); if (date1.equals(date2)) { System.out.println("日期相等"); } else { System.out.println("日期不相等"); } // 时间 LocalTime date1 = LocalTime.of(9, 46); LocalTime date2 = LocalTime.now(); if (date1.equals(date2)) { System.out.println("时间相等"); } else { System.out.println("时间不相等"); } // 时间加日期 LocalDateTime date1 = LocalDateTime.of(2019,1,17,9,47); LocalTime date2 = LocalTime.now(); if (date1.equals(date2)) { System.out.println("时间加日期相等"); } else { System.out.println("时间加日期不相等"); }
5、检查周几这种周期性的事件
DayOfWeek of = DayOfWeek.THURSDAY; DayOfWeek from = DayOfWeek.from(LocalDate.now()); if (from.equals(of)) { System.out.println("今天是周四"); } else { System.out.println("今天不是周四"); } DayOfWeek from1 = DayOfWeek.from(LocalDate.of(2019, 1, 10)); if (from1.equals(of)) { System.out.println("该日期是周四"); } else { System.out.println("该日期不是周四"); }
6、指定时间之后或之前XXX的时间
LocalDateTime now = LocalDateTime.now(); System.out.println("当前时间"+now); System.out.println("当前时间之前2天:"+now.minusDays(2)); System.out.println("当前时间之后2天:"+now.plusDays(2)); System.out.println("当前时间之前2周:"+now.minusWeeks(2)); System.out.println("当前时间之后2周:"+now.plusWeeks(2)); System.out.println("当前时间之前2月:"+now.minusMonths(2)); System.out.println("当前时间之后2月:"+now.plusMonths(2)); System.out.println("当前时间之前2年:"+now.minusYears(2)); System.out.println("当前时间之后2年:"+now.plusYears(2)); System.out.println("当前时间之前2小时:"+now.minusHours(2)); System.out.println("当前时间之后2小时:"+now.plusHours(2)); System.out.println("当前时间之前2分钟:"+now.minusMinutes(2)); System.out.println("当前时间之后2分钟:"+now.plusMinutes(2)); System.out.println("当前时间之前2秒:"+now.minusSeconds(2)); System.out.println("当前时间之后2秒:"+now.plusSeconds(2));
7、比较时间的先后
LocalDateTime now = LocalDateTime.now(); System.out.println("当前时间:"+now); LocalDateTime appointTime = LocalDateTime.of(2019, 1, 17, 9, 32, 12); System.out.println("指定时间:"+appointTime); System.out.println(now.isAfter(appointTime)); System.out.println(now.isBefore(appointTime));
8、格式化时间格式及按照一定格式解析
LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFdonateTime.parse(format, formatter); LocalDate localDate = LocalDate.parse(format2, formatter2); LocalTime localTime = LocalTime.parse(format3, formatter3); System.out.println("解析1:" + localDateTime); System.out.println("解析2:" + localDate); System.out.println("解析3:" + localTime);
9、LocalDateTime
//根据指定的毫秒数获取LocalDateTime对象
Long nowMilli = new Date().getTime(); LocalDateTime localDateTime = Instant.ofEpochMilli(nowMilli).atZone(ZoneId.of("+8")).toLocalDateTime(); System.out.println(localDateTime);
//通过LocalDateTime对象获取毫秒数
LocalDateTime now = LocalDateTime.now(); long l = now.atZone(ZoneId.of("+8")).toInstant().toEpochMilli(); System.out.println(l);
//通过LocalDateTime对象获取秒数
LocalDateTime now = LocalDateTime.now();
long second = now.toEpochSecond(ZoneOffset.of("+8"));
System.out.println(second);
//判断指定日期是否是闰年
LocalDate now = LocalDate.now();
if (now.isLeapYear()) {
System.out.println("now是闰年");
} else {
System.out.println("now不是闰年");
}
注:+8 指东八区,北京时间
10、使用默认格式解析时间(LocalDateTime对象可解析的格式yyyy-MM-ddTHH:mm:ss)--不是空格了,是T
LocalDateTime dateTime = LocalDateTime.parse("2019-01-14T12:04:12"); int monthValue = dateTime.getMonthValue(); int minute = dateTime.getMinute(); System.out.println(monthValue+":"+minute); LocalTime time = LocalTime.parse("19:14:25"); int hour = time.getHour(); System.out.println(hour); LocalDate date = LocalDate.parse("2019-01-12"); int year = date.getYear(); System.out.println(year);