Java 8 时间日期使用
一、日期时间
Java 8通过发布新的Date-Time API (JSR 310)来进一步加强对日期与时间的处理。java.util.Date和SimpleDateFormatter都不是线程安全的,而LocalDate和LocalTime和最基本的String一样,是不变类型,不但线程安全,而且不能修改。以下为一些常用时间对象:
- Instant:表示时刻,不直接对应年月日信息,需要通过时区转换
- LocalDateTime: 表示与时区无关的日期和时间信息,不直接对应时刻,需要通过时区转换
- LocalDate:表示与时区无关的日期,与LocalDateTime相比,只有日期信息,没有时间信息
- LocalTime:表示与时区无关的时间,与LocalDateTime相比,只有时间信息,没有日期信息
- ZonedDateTime: 表示特定时区的日期和时间
- ZoneId/ZoneOffset:表示时区
1、LocalDate
LocalDate now = LocalDate.now(); LocalDate now1 = LocalDate.now(Clock.systemUTC()); System.out.println(now); System.out.println(now1); LocalDate of = LocalDate.of(2019, 3, 6); //严格按照ISO yyyy-MM-dd验证,03写成3都不行 LocalDate parse = LocalDate.parse("2019-03-06"); System.out.println(of); System.out.println(parse); System.out.println("**************now****************"); //当前开始时间 System.out.println(now.atStartOfDay()); //当月第一天日期 System.out.println(now.with(TemporalAdjusters.firstDayOfMonth())); //本月第二天日期 System.out.println(now.withDayOfMonth(2)); //当月最后一天 System.out.println(now.with(TemporalAdjusters.lastDayOfMonth())); System.out.println(now.getDayOfMonth()); //当月下一天 System.out.println(now.plusDays(1)); //当月上一天 System.out.println(now.minusDays(1)); System.out.println(now.getDayOfWeek()); //当月下一周 System.out.println(now.plusWeeks(1)); //当月上一周 System.out.println(now.minusWeeks(1)); System.out.println(now.getMonth() + "-" + now.getMonthValue()); //当月下一个月 System.out.println(now.plusMonths(1)); //当月上一个月 System.out.println(now.minusMonths(1)); //时间比较 System.out.println(now.isEqual(LocalDate.of(2019, 03, 06)));
2、LocalTime
LocalTime now = LocalTime.now(); //指定时区 LocalTime now1 = LocalTime.now(Clock.system(ZoneId.systemDefault())); LocalTime now2 = LocalTime.now(Clock.systemUTC()); System.out.println(now); System.out.println(now1); System.out.println(now2); System.out.println("************now************"); //清除毫秒位 System.out.println(now.withNano(0)); //获取当前的小时 System.out.println(now.getHour()); //解析时间时间也是按照ISO格式识别,但可以识别以下3种格式: 12:00 12:01:02 12:01:02.345 System.out.println(LocalTime.parse("11:58:12")); //时间比较 LocalTime other = LocalTime.of(13, 45, 59); System.out.println(now.isBefore(other)); System.out.println(now.isAfter(other));
3、LocalDateTime
LocalDateTime now = LocalDateTime.now(); LocalDateTime now1 = LocalDateTime.now(Clock.system(ZoneId.systemDefault())); LocalDateTime now2 = LocalDateTime.now(Clock.systemUTC()); System.out.println(now); System.out.println(now1); System.out.println(now2);
//时间格式转换 System.out.println(now.format(DateTimeFormatter.ofPattern(PATTERN_1))); System.out.println(now.format(DateTimeFormatter.ofPattern(PATTERN_2))); System.out.println(now.format(DateTimeFormatter.ofPattern(PATTERN_3)));
4、Date转换为LocalDateTime
/** * Date转换为LocalDateTime * @return LocalDateTime */ public static LocalDateTime date2LocalDateTime() { Date date = new Date(); LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()); return localDateTime; }
5、LocalDateTime转换为Date
/** * LocalDateTime转换为Date * @return Date */ public static Date localDateTime2Date() { LocalDateTime now = LocalDateTime.now(); Instant instant = now.atZone(ZoneId.systemDefault()).toInstant(); Date date = Date.from(instant); return date; }
6、秒与纳秒级别上的一段时间间隔
/** * 秒与纳秒级别上的一段时间 */ public static void duration() { LocalDateTime from = LocalDateTime.of(2018, Month.OCTOBER, 1, 0, 0, 0); LocalDateTime to = LocalDateTime.of(2019, Month.MARCH, 6, 23, 59, 59); Duration between = Duration.between(from, to); System.out.println(between.toDays()); System.out.println(between.toHours()); }