威慑么需要LocalDate,LocalTime,LocalDateTime;(一定要看到最后)
- Date如果不格式化,打印出日期可读性差
Mon Nov 23 20:37:06 CST 2020
- 使用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需要格外注意SimpleDateFormat除了format是线程不安全以外,parse方法也是线程不安全的,parse方法实际调用alb.establish(calendar).getTime()方法来解析;
多线程并发如何保证线程安全?
避免线程之间共享一个SimpleDateFormat对象,
- 每个线程使用时都创建一次SimpleDateFormat对象=>
- 创建和销毁对象的开销大-对使用format和parse的方法进行枷锁=>
- 线程阻塞性能差-使用ThreadLocal保证每个线程最多只创建一次SimpleDateFormat对象=>
BEGIN,一起使用java8的全新日期和时间API
https://nowjava.com/docs/java-api-11/java.base/java/time/temporal/ChronoField.html
Localdate:只会获取年月日
- 创建Localdate 获取年,月,日,星期几
//获取当前年月日
LocalDate localDate = LocalDate.now();
//构造指定的年月日
LocalDate.of(2020,11,23);
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(21,40,22);
//获取当前时分秒
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.getSecond();
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 / LocalTime
LocalDate localDate2 = localDateTime.toLocalDate();
LocalTime localTime2 = localDateTime.toLocalTime();
Instant 获取秒数
//创建Instant对象
Instant instant = Instant.now();
//获取秒数
long currentSecond = instant.getEpochSecond();
//获取毫秒数
long currentMilli = instant.toEpochMilli();
毫秒数,使用System.currentTimeMillis()
来得更为方便
格式化时间
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
是线程安全的