![c70d7c246976df4ca2ecad1898c05deb.png](https://img-blog.csdnimg.cn/img_convert/c70d7c246976df4ca2ecad1898c05deb.png)
“ 在项目开发过程中经常遇到时间处理,但是你真的用对了吗,理解阿里巴巴开发手册中禁用static修饰SimpleDateFormat吗
”
通过阅读本篇文章你将了解到:
- 为什么需要
LocalDate
、LocalTime
、LocalDateTime
【java8新提供的类】 java8
新的时间API
的使用方式,包括创建、格式化、解析、计算、修改
为什么需要LocalDate、LocalTime、LocalDateTime
Date
如果不格式化,打印出的日期可读性差
Tue Sep 10 09:34:04 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
是共享变量,并且这个共