java8的时间和Date的对比
java8提供了新的时间接口。相对Date,Calendar,个人感觉最大的好处是对时间操作的学习成本很低,比Calendar低。
1. LocalDate,LocalTime,LocalDateTime
LocalDate 代表日期,LocalTime表示时刻,类似11:23这样的时刻。 LocalDateTime就是前面2个的结合,这个可以从java.time.LocalDateTime#toString的代码看出一二:
1@Override 2public String toString() { 3 return date.toString() + 'T' + time.toString(); 4}
date,time 在java.time.LocalDateTime中
1/** 2 * The date part. 3 */ 4private final LocalDate date; 5/** 6 * The time part. 7 */ 8private final LocalTime time;
实际使用中,计算日期就用LocalDate,计算日期加时刻用LocalDateTime,如果只有时刻就是LocalTime(感觉在说废话)
这三个的用法基本上一样,通过方法名就知道用法那种
1.1 获取当前时间的对象
1LocalDateTime localDateTime = LocalDateTime.now(); 2Date date = new Date();
localDateTime相比Date更像是一个工具类,就是为了时间操作使用。其构造方法是私有的。
1.2 从字符串中解析
字符串 2019-01-11 解析成时间对象
1String str = "2019-01-11"; 2DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); 3LocalDate localDate = LocalDate.parse(str, formatter); 4 5SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 6try { 7 Date date = simpleDateFormat.parse(str); 8} catch (ParseException e) { 9 e.printStackTrace(); 10}
DateTimeFormatter的包路径是java.time.format和LocalDate一样在java.time下面,而SimpleDateFormat和Date是不同的。所以当判断引入路径的时候更容易判断。
当解析失败的时候,两个异常的抛出不一样,DateTimeFormatter抛出的是DateTimeParseException,继承自RuntimeException,而ParseException明显继承的是Exception。
个人感觉这个思路是,前者如果抛出异常那就是编程上错误,而后者则是的程序代码的不稳定性。我更倾向于第一种的异常设计,应该加强对入参的检测判断,而不是通过捕获异常去处理入参的错误。(类似NumberFormatException)
1.3 LocalDate比Date更强的初始化时间
Date 设置某个日期,基本上3个方式,时间戳/Calendar/字符串解析。相对的LocalDate就简单了很多
LocalDate.of(2019,1,12);
其他的也一样

1.4 时间戳的转换
在这里时间戳的转换不如Date直接。主要因为LocalDate本身是没有时区的。
-
时间戳传LocalDateTime
long timestamp = System.currentTimeMillis(); Instant instant = Instant.ofEpochMilli(timestamp); LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
-
LocalDateTime转时间戳LocalDateTime dateTime = LocalDateTime.now(); dateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli(); dateTime.toInstant(ZoneOffset.of("+08:00")).toEpochMilli(); dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
关于时区的计算也很简单,就是相差几个小时就加上多少秒

有些时区计算的时候,不妨自己加时间也一样,elasticsearch+logstash设置@timestamp时间是默认UTC Z的时间,和咱们差了8个小时
LocalDateTime.parse(json.getString("@timestamp"), DateTimeFormatter.ISO_DATE_TIME).plusHours(8L)
1.5 和Date互转
1import java.time.Instant; 2import java.util.Date; 3 4public class Main { 5 public static void main(String[] args) { 6 Date dt = new Date(); 7 System.out.println("Date: " + dt); 8 9 Instant in = dt.toInstant(); 10 System.out.println("Instant: " + in); 11 12 Date dt2 = Date.from(in); 13 System.out.println("Date: " + dt2); 14 } 15}
Instant 和 LocalDate或LocalDateTime 就不赘述了...
代码来自 Java 日期时间传统互操作性
1.6 更好的理解和操作方式
Date、Calendar的操作,例如设置月份,day of week 都有些让人迷惑,例如1月的定义是0,周一是0。1号好像也是0吧(我真没咋用过这东西,现用现百度...
LocalDate感觉好多了。例如DayOfWeek是枚举类型。使用枚举就不会理解错了吧 
很多日期和时间操作,无非就是加减时间和比较.
使用‘加’的示例:

不用再去使用一个不熟悉的Calendar去操作了(Calendar提供的接口都是啥玩意,get,set的)
1Calendar cal = Calendar.getInstance(); 2cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1)
2. 线程安全性比较
LocalDate...系列是线程安全的
额..每一个字段都用了final关键字了,都变不了... 所以进行操作后都是返回新的copy对象 
至于说Date线程不安全,get,set的肯定在多线程的时候容易出现问题,不过set方法已经都@Deprecated废弃了。当然不是因为线程安全问题废弃的,是因为有了更好的替代
Calendar.set(Calendar.DAY_OF_MONTH, int date)
不过感觉还是不如这个更清晰明了
LocalDate.of(2019,1,12);
2.1 SimpleDateFormat的线程安全性
在一定负载情况下,SimpleDateFormat会出问题的。简单测试一下
1package open.note; 2 3import java.text.SimpleDateFormat; 4import java.time.LocalDateTime; 5import java.time.format.DateTimeFormatter; 6import java.util.Date; 7import java.util.concurrent.CountDownLatch; 8import java.util.function.Consumer; 9 10public class UnSafeTest { 11 12 private static String time = "2019-01-11 11:11:11"; 13 private static long timestamp = 1547176271000L; 14 private static LocalDateTime dateTime = LocalDateTime.of(2019,1,11,11,11,11); 15 private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 16 private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); 17 18 public static void main(String[] args) { 19 dateFormatTest((obj)->{ 20 try { 21 Date date = dateFormat.parse(time); 22 if (date.getTime() != timestamp){ 23 System.out.println(date); 24 } 25 } catch (Exception e) { 26 System.out.println(e.getMessage()); 27 } 28 }); 29 System.out.println("---------------"); 30 dateFormatTest((obj)->{ 31 try { 32 LocalDateTime dateTime = LocalDateTime.parse(time,formatter); 33 if (!dateTime.isEqual(UnSafeTest.dateTime)){ 34 System.out.println(dateTime); 35 } 36 } catch (Exception e) { 37 System.out.println(e.getMessage()); 38 } 39 }); 40 } 41 private static void dateFormatTest(Consumer runnable){ 42 CountDownLatch countDownLatch = new CountDownLatch(1000); 43 for (int i = 0; i < 1000; i++) { 44 new Thread(()->{ 45 runnable.accept(null); 46 countDownLatch.countDown(); 47 }).start(); 48 } 49 try { 50 countDownLatch.await(); 51 } catch (InterruptedException e) { 52 e.printStackTrace(); 53 } 54 } 55} 56
输出结果
1multiple points 2multiple points 3empty String 4Sat Jan 11 11:11:11 CST 111 5Fri Jan 04 11:11:11 CST 2019 6For input string: "" 7Mon Dec 31 11:11:11 CST 2018 8Mon Dec 31 11:11:11 CST 2018 9For input string: "" 10Tue Jan 11 11:11:11 CST 42101 11---------------
测试过程中,SimpleDateFormat 1000个线程里,有5次,时间解析错了,5次异常了(时间错了,比抛出异常还可怕)
DateTimeFormatter只是对比参考一下,未出现异常(人家已经声明是线程安全了...)
当然SimpleDateFormat线程不安全应该人尽皆知的,但依然有不安全的使用,但每次使用都new一个实例,当负载大的时候也不好。所以一个线程一个SimpleDateFormat实例应该可以的。
最后
java8 对时间操作的类还有很多 到java.time包下去看看,以后总会用得到的地方。
Instant:时间戳
Duration:持续时间,时间差
LocalDate:只包含日期,比如:2016-10-20
LocalTime:只包含时间,比如:23:12:10
LocalDateTime:包含日期和时间,比如:2016-10-20 23:14:21
Period:时间段
ZoneOffset:时区偏移量,比如:+8:00
ZonedDateTime:带时区的时间
Clock:时钟,比如获取目前美国纽约的时间