在项目开发过程中经常遇到时间处理,但是你真的用对了吗,理解阿里巴巴开发手册中禁用static修饰SimpleDateFormat吗
通过阅读本篇文章你将了解到:
LocalDate
、LocalTime
、LocalDateTime
【java8新提供的类】java8
新的时间API
的使用方式,包括创建、格式化、解析、计算、修改Date
如果不格式化,打印出的日期可读性差
Tue Sep 10 09:34:04 CST 2019SimpleDateFormat
对时间进行格式化,但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
需格外注意SimpleDateFormat
除了format
是线程不安全以外,parse
方法也是线程不安全的。parse
方法实际调用alb.establish(calendar).getTime()
方法来解析,alb.establish(calendar)
方法里主要完成了但是这三步不是原子操作
多线程并发如何保证线程安全 - 避免线程之间共享一个SimpleDateFormat
对象,每个线程使用时都创建一次SimpleDateFormat
对象 => 创建和销毁对象的开销大 - 对使用format
和parse
方法的地方进行加锁 => 线程阻塞性能差 - 使用ThreadLocal
保证每个线程最多只创建一次SimpleDateFormat
对象 => 较好的方法
Date
对时间处理比较麻烦,比如想获取某年、某月、某星期,以及n
天以后的时间,如果用Date
来处理的话真是太难了,你可能会说Date
类不是有getYear
、getMonth
这些方法吗,获取年月日很Easy
,但都被弃用了啊只会获取年月日
LocalDate
//获取当前年月日 LocalDate localDate = LocalDate.now(); //构造指定的年月日 LocalDate localDate1 = LocalDate.of(2019, 9, 10);只会获取几点几分几秒
LocalTime
LocalTime localTime = LocalTime.of(13, 51, 10); LocalTime localTime1 = LocalTime.now();获取年月日时分秒,等于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
对象
Instant instant = Instant.now();个人觉得如果只是为了获取秒数或者毫秒数,使用System.currentTimeMillis()
来得更为方便
LocalDate
、LocalTime
、LocalDateTime
、Instant
为不可变对象,修改这些对象对象会返回一个副本
还可以修改月、日
比如有些时候想知道这个月的最后一天是几号、下个周末是几号,通过提供的时间和日期API可以很快得到答案
LocalDate localDate = LocalDate.now();
LocalDate localDate1 = localDate.with(firstDayOfYear());
比如通过firstDayOfYear()
返回了当前日期的第一天日期,还有很多方法这里不在举例说明
LocalDate localDate = LocalDate.of(2019, 9, 10);
String s1 = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
String s2 = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
//自定义格式化
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
String s3 = localDate.format(dateTimeFormatter);
DateTimeFormatter
默认提供了多种格式化方式,如果默认提供的不能满足要求,可以通过DateTimeFormatter
的ofPattern
方法创建自定义格式化方式
LocalDate localDate1 = LocalDate.parse("20190910", DateTimeFormatter.BASIC_ISO_DATE);
LocalDate localDate2 = LocalDate.parse("2019-09-10", DateTimeFormatter.ISO_LOCAL_DATE);
和SimpleDateFormat
相比,DateTimeFormatter
是线程安全的
LocalDateTime`:`Date`有的我都有,`Date`没有的我也有,日期选择请`Pick Me
====================== Update On 2019/09/18 =================
SpringBoot中应用LocalDateTime
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有