日期中用 YYYY 一定会报错吗?

今年春节真是打破了 N 多传统,很多人都是在家连门都没出过,从今天开始也要开始在家远程办公了,因为和小伙伴合作开发一个功能,但是接口目前还没给到,然后记得今年元旦前后,关于 YYYY 报错的问题,突然火了,据说有 N 多程序员被火速召回公司改 bug,所以决定写篇小文章说说这个问题:YYYY 一定会报错吗? 首先需要说明的是:我用的 JDK 版本为:jdk-8u131-macosx-x64,所以具体表现为应该显示:2019-12-31,结果确是:2020-12-31,另外经过我的测试其实不仅 format 的时候报错,parse 的时候同样也会报错,我写了一段示例代码如下: package cn.bridgeli.demo; import org.joda.time.DateTime; import org.junit.Test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * Created by bridgeli on 2019/02/03. */ public class DateTest { @Test public void testSimpleDateFormat() throws ParseException { SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateTime dateTime1 = new DateTime(2019, 12, 31, 23, 59, 59); String date1 = simpleDateFormat1.format(dateTime1.toDate()); System.out.println("date1: " + date1); SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd"); DateTime dateTime2 = new DateTime(2020, 01, 01, 23, 59, 59); String date2 = simpleDateFormat2.format(dateTime2.toDate()); System.out.println("date2: " + date2); SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); DateTime dateTime3 = new DateTime(2019, 12, 31, 23, 59, 59); String date3 = simpleDateFormat3.format(dateTime3.toDate()); System.out.println("date3: " + date3); SimpleDateFormat simpleDateFormat4 = new SimpleDateFormat("YYYY-MM-dd"); DateTime dateTime4 = new DateTime(2020, 01, 01, 23, 59, 59); String date4 = simpleDateFormat4.format(dateTime4.toDate()); System.out.println("date4: " + date4); SimpleDateFormat simpleDateFormat5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date5 = simpleDateFormat5.parse("2019-12-31 23:59:59"); System.out.println("date5: " + date5); SimpleDateFormat simpleDateFormat6 = new SimpleDateFormat("yyyy-MM-dd"); Date date6 = simpleDateFormat6.parse("2020-01-01"); System.out.println("date6: " + date6); SimpleDateFormat simpleDateFormat7 = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); Date date7 = simpleDateFormat7.parse("2019-12-31 23:59:59"); System.out.println("date7: " + date7); SimpleDateFormat simpleDateFormat8 = new SimpleDateFormat("YYYY-MM-dd"); Date date8 = simpleDateFormat8.parse("2020-01-01"); System.out.println("date8: " + date8); } } 上面这段代码的输出结果为: ...

February 3, 2020 · 3 min · 541 words · Bridge Li

ThreadLocal类之简单应用示例

在日常开发的系统中,日期处理是非常非常用的一个功能,处理的日期的时候就需要用到SimpleDateFormat对象,但是我们都知道SimpleDateFormat本身不是线程安全的(如果不知道的请看源码),所以就需要频繁创建SimpleDateFormat这个对象。但是我们知道创建这个对象本身不仅是很费时的,而且创建的这些对象存活期很短,导致内存中大量这样的对象需要被GC,所以我们自然而然的想到使用ThreadLocal来给每个线程缓存一个SimpleDateFormat实例,提高性能。下面是一个具体的实现的小例子,其实不仅针对SimpleDateFormat对象,对于数据库连接等等都可以这么使用。 package cn.bridgeli.demo; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Map; public class DateFormatFactory { private static final Map<DatePatternEnum, ThreadLocal<DateFormat>> pattern2ThreadLocal; static { DatePatternEnum[] patterns = DatePatternEnum.values(); int len = patterns.length; pattern2ThreadLocal = new HashMap<DatePatternEnum, ThreadLocal<DateFormat>>(len); for (int i = 0; i < len; i++) { DatePatternEnum datePatternEnum = patterns[i]; final String pattern = datePatternEnum.pattern; pattern2ThreadLocal.put(datePatternEnum, new ThreadLocal<DateFormat>() { @Override protected DateFormat initialValue() { return new SimpleDateFormat(pattern); } }); } } // 获取DateFormat public static DateFormat getDateFormat(DatePatternEnum patternEnum) { ThreadLocal<DateFormat> threadDateFormat = pattern2ThreadLocal.get(patternEnum); // 不需要判断threadDateFormat是否为空 return threadDateFormat.get(); } } 对应的时间枚举类(如果还有其他格式的时间需要处理,可以直接在这个类里面添加即可): ...

June 18, 2017 · 1 min · 114 words · Bridge Li