在Java 8以前,全部关于时间和日期的API都存在各类使用方面的缺陷,所以建议使用新的时间和日期API,分别从旧的时间和日期的API的缺点以及解决方法、Java 8 新的时间和日期API进行讲解。java
Java 的 java.util.Date 和 java.util.Calendar 类易用性差,不支持时区,并且都不是线程安全的。api
Date若是不格式化,打印出的日期可读性差。安全
Thu Sep 12 13:47:34 CST 2019
能够使用 SimpleDateFormat 对时间进行格式化,但 SimpleDateFormat 是线程不安全的,SimpleDateFormat 的 format 方法源码以下:多线程
private StringBuffer format(Date date, StringBuffer toAppendTo, FieldDelegate delegate) { // Convert input date to time field list calendar.setTime(date); boolean useDateFormatSymbols = useDateFormatSymbols(); for (int i = 0; i < compiledPattern.length; ) { int tag = compiledPattern[i] >>> 8; int count = compiledPattern[i++] & 0xff; if (count == 255) { count = compiledPattern[i++] << 16; count |= compiledPattern[i++]; } switch (tag) { case TAG_QUOTE_ASCII_CHAR: toAppendTo.append((char)count); break; case TAG_QUOTE_CHARS: toAppendTo.append(compiledPattern, i, count); i += count; break; default: subFormat(tag, count, delegate, toAppendTo, useDateFormatSymbols); break; } } return toAppendTo; }
其中 calendar 是共享变量,而且这个共享变量没有作线程安全控制。当多个线程同时使用相同的 SimpleDateFormat 对象【如用static修饰的 SimpleDateFormat 】调用format方法时,多个线程会同时调用 calendar.setTime 方法,可能一个线程刚设置好 time 值另外的一个线程立刻把设置的 time 值给修改了致使返回的格式化时间多是错误的。并发
在多并发状况下使用 SimpleDateFormat 需注意。app
SimpleDateFormat 除了 format 是线程不安全之外,parse 方法也是线程不安全的。parse 方法实际调用 alb.establish(calendar).getTime() 方法来解析,alb.establish(calendar) 方法里主要完成了工具
可是这三步不是原子操做,致使解析出来的时间能够是错误的。性能
Date对时间处理比较麻烦,好比想获取某年、某月、某星期,以及 n 天之后的时间,若是用Date来处理的话真是太难了,而且 Date 类的 getYear、getMonth 这些方法都被弃用了。线程
避免线程之间共享一个 SimpleDateFormat 对象,每一个线程使用时都建立一次 SimpleDateFormat 对象 => 建立和销毁对象的开销大code
对使用 format 和 parse 方法的地方进行加锁 => 线程阻塞性能差
使用 ThreadLocal 保证每一个线程最多只建立一次 SimpleDateFormat 对象 => 较好的方法
Java 8的日期和时间类包含 LocalDate、LocalTime、Instant、Duration 以及 Period,这些类都包含在 java.time 包中,Java 8 新的时间API的使用方式,包括建立、格式化、解析、计算、修改,下面咱们看下如何去使用。
LocalDate 只会获取年月日
// 建立 LocalDate // 获取当前年月日 LocalDate localDate = LocalDate.now(); // 构造指定的年月日 LocalDate localDate1 = LocalDate.of(2019, 9, 12); // 获取年、月、日、星期几 int year = localDate.getYear(); int year1 = localDate.get(ChronoField.YEAR); Month month = localDate.getMonth(); int month1 = localDate.get(ChronoField.MONTH_OF_YEAR); int day = localDate.getDayOfMonth(); int day1 = localDate.get(ChronoField.DAY_OF_MONTH); DayOfWeek dayOfWeek = localDate.getDayOfWeek(); int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
LocalTime 只会获取时分秒
// 建立 LocalTime LocalTime localTime = LocalTime.of(14, 14, 14); LocalTime localTime1 = LocalTime.now(); // 获取小时 int hour = localTime.getHour(); int hour1 = localTime.get(ChronoField.HOUR_OF_DAY); // 获取分 int minute = localTime.getMinute(); int minute1 = localTime.get(ChronoField.MINUTE_OF_HOUR); // 获取秒 int second = localTime.getMinute(); int second1 = localTime.get(ChronoField.SECOND_OF_MINUTE);
LocalDateTime 获取年月日时分秒,至关于 LocalDate + LocalTime
// 建立 LocalDateTime LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime1 = LocalDateTime.of(2019, Month.SEPTEMBER, 10, 14, 46, 56); LocalDateTime localDateTime2 = LocalDateTime.of(localDate, localTime); LocalDateTime localDateTime3 = localDate.atTime(localTime); LocalDateTime localDateTime4 = localTime.atDate(localDate); // 获取LocalDate LocalDate localDate2 = localDateTime.toLocalDate(); // 获取LocalTime LocalTime localTime2 = localDateTime.toLocalTime();
Instant 获取秒数,用于表示一个时间戳(精确到纳秒)
若是只是为了获取秒数或者毫秒数,能够使用 System.currentTimeMillis()。
// 建立Instant对象 Instant instant = Instant.now(); // 获取秒数 long currentSecond = instant.getEpochSecond(); // 获取毫秒数 long currentMilli = instant.toEpochMilli();
Duration 表示一个时间段
// Duration.between()方法建立 Duration 对象 LocalDateTime from = LocalDateTime.of(2017, Month.JANUARY, 1, 00, 0, 0); // 2017-01-01 00:00:00 LocalDateTime to = LocalDateTime.of(2019, Month.SEPTEMBER, 12, 14, 28, 0); // 2019-09-15 14:28:00 Duration duration = Duration.between(from, to); // 表示从 from 到 to 这段时间 long days = duration.toDays(); // 这段时间的总天数 long hours = duration.toHours(); // 这段时间的小时数 long minutes = duration.toMinutes(); // 这段时间的分钟数 long seconds = duration.getSeconds(); // 这段时间的秒数 long milliSeconds = duration.toMillis(); // 这段时间的毫秒数 long nanoSeconds = duration.toNanos(); // 这段时间的纳秒数
修改 LocalDate、LocalTime、LocalDateTime、Instant。
LocalDate、LocalTime、LocalDateTime、Instant 为不可变对象,修改这些对象对象会返回一个副本。
增长、减小年数、月数、天数等,以LocalDateTime为例:
LocalDateTime localDateTime = LocalDateTime.of(2019, Month.SEPTEMBER, 12, 14, 32, 0); // 增长一年 localDateTime = localDateTime.plusYears(1); localDateTime = localDateTime.plus(1, ChronoUnit.YEARS); // 减小一个月 localDateTime = localDateTime.minusMonths(1); localDateTime = localDateTime.minus(1, ChronoUnit.MONTHS); // 经过with修改某些值 // 修改年为2020 localDateTime = localDateTime.withYear(2020); localDateTime = localDateTime.with(ChronoField.YEAR, 2020); // 时间计算 // 获取该年的第一天 LocalDate localDate = LocalDate.now(); LocalDate localDate1 = localDate.with(firstDayOfYear());
TemporalAdjusters 包含许多静态方法,能够直接调用,如下列举一些:
方法名 | 描述 |
---|---|
dayOfWeekInMonth | 返回同一个月中每周的第几天 |
firstDayOfMonth | 返回当月的第一天 |
firstDayOfNextMonth | 返回下月的第一天 |
firstDayOfNextYear | 返回下一年的第一天 |
firstDayOfYear | 返回本年的第一天 |
firstInMonth | 返回同一个月中第一个星期几 |
lastDayOfMonth | 返回当月的最后一天 |
lastDayOfNextMonth | 返回下月的最后一天 |
lastDayOfNextYear | 返回下一年的最后一天 |
lastDayOfYear | 返回本年的最后一天 |
lastInMonth | 返回同一个月中最后一个星期几 |
next / previous | 返回后一个/前一个给定的星期几 |
nextOrSame / previousOrSame | 返回后一个/前一个给定的星期几,若是这个值知足条件,直接返回 |
格式化时间
LocalDate localDate = LocalDate.of(2019, 9, 12); String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE); String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE); // 自定义格式化 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String s3 = localDate.format(dateTimeFormatter);
解析时间
LocalDate localDate1 = LocalDate.parse("20190912", DateTimeFormatter.BASIC_ISO_DATE); LocalDate localDate2 = LocalDate.parse("2019-09-12", DateTimeFormatter.ISO_LOCAL_DATE);
和 SimpleDateFormat 相比,DateTimeFormatter 是线程安全的。
Instant 的精确度更高,能够精确到纳秒级。
Duration 能够便捷获得时间段内的天数、小时数等。
LocalDateTime 可以快速地获取年、月、日、下一月等。
TemporalAdjusters 类中包含许多经常使用的静态方法,避免本身编写工具类。