在线网站软件免费下载安装,新建网站怎么保存,如何建立团购网站,网站域名设计Jdk1.8之时间处理该文章已经同步到Github#xff1a;https://github.com/stackInk/makerstack1. 传统时间处理的问题1.1 多线程环境下的SimpleDateFormat当多个线程使用同一个时间处理对象进行对日期的格式化的时候#xff0c;会出现java.lang.NumberFormatException: multip…Jdk1.8之时间处理该文章已经同步到Githubhttps://github.com/stackInk/makerstack1. 传统时间处理的问题1.1 多线程环境下的SimpleDateFormat当多个线程使用同一个时间处理对象进行对日期的格式化的时候会出现java.lang.NumberFormatException: multiple points。主要原因是由于SimpleDateFormat是线程不安全的当线程共享的时候会引发这个异常。1.1.1 代码演示SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyyMMdd);
//线程池中线程共享了SimpleDateFormat引发线程不安全
CallableString callable () - simpleDateFormat.parse(20200402).toString();ExecutorService executorService Executors.newFixedThreadPool(10);
ListFutureString list new LinkedList();for (int i 0; i 10; i) {FutureString submit executorService.submit(callable);list.add(submit);
}
for (FutureString stringFuture : list) {String s stringFuture.get();System.out.println(s);
}
executorService.shutdown();
解决方法线程不共享变量SimpleDateFormat每一个线程在进行日期格式化的时候都自己创建一个ExecutorService executorService Executors.newFixedThreadPool(10);
ListFutureString list new LinkedList();
for (int i 0; i 10; i) {FutureString submit executorService.submit(new MyCallable01(20200403));list.add(submit);
}
for (FutureString stringFuture : list) {String s stringFuture.get();System.out.println(s);}
executorService.shutdown();class MyCallable01 implements CallableString{private String date ;public MyCallable01(String date) {this.date date;}Overridepublic String call() throws Exception {SimpleDateFormat simpleDateFormat new SimpleDateFormat(yyyyMMdd);return simpleDateFormat.parse(date).toString();}
}
通过ThreadLocal为每一个线程绑定一个SimpleDateFormateFutureString submit executorService.submit(() - ResolveByThreadLocal.converDateStrToDate(20200405));public class ResolveByThreadLocal {//创建一个绑定每一个变量的ThreadLocalprivate static final ThreadLocalSimpleDateFormat threadLocal new ThreadLocal();public static String converDateStrToDate(String date) throws ParseException {SimpleDateFormat simpleDateFormat threadLocal.get(); ;if(simpleDateFormat null){simpleDateFormat new SimpleDateFormat(yyyyMMdd);threadLocal.set(simpleDateFormat);}Date parse simpleDateFormat.parse(date);return parse.toString() ;}
}2. 1.8时间处理对于时间的处理均在java.time包及其子包中且线程安全java.time包下存放了进行时间处理的各种类 Instant获取本地时间的时间戳LocalDate获取本地时间的日期LocalTime获取本地时间的时间LocalDateTime获取本地时间的日期和时间Duration计算两个日期之间的间隔Period计算两个时间的间隔OffsetDateTime对日期和时间进行偏移量计算offsetTime对时间进行偏移量计算ZoneId各种时区代码ZoneOffset市区偏移量计算ZonedDateTimejava.time.chrono不同地区时间记时方式java.time.temporal对时间进行一些调整的包java.time.format对时间进行格式化2.1 LocalDate、LocalTime、LocalDateTime三者的使用方式完全相同输出的结果不同now获取本地时间LocalDateTime now LocalDateTime.now();System.out.println(now);System.out.println(now.getYear());System.out.println(now.getMonthValue());//直接获取月份的值System.out.println(now.getDayOfMonth());System.out.println(now.getHour());System.out.println(now.getMinute());System.out.println(now.getSecond());输出:
2020-04-03T10:25:29.906
2020
4
3
10
25
29
of()传入指定的日期和时间对时间进行偏移量加计算对事件进行偏移量减运算当前时间与另一个时间的比较将月份天数年份天数月份等修改为指定的值返回一个新的LocalDateTime对象get方法format(DateTimeFormatter formatter)对日期进行格式化until返回两个日期之间的Period对象isLeapYear判断是否为闰年2.2 Instant时间戳以Unix元年(传统设定为UTC时区1970年1月1日)开始所经历的描述进行运算获取当前时间的时间戳toEpochMilli获取当前时间的秒getEpochSecond对时间进行偏移Instant.now().ofHours(ZoneOffset.ofHours(int hours))2.3 TemporalAdjuster 时间校正器主要通过TemporalAdjusters工具类获取到TemporalAdjuster实例对象LocalDateTime now LocalDateTime.now();//直接调用JDK提供的时间校正器LocalDateTime with now.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));System.out.println(with);//自定义一个时间校正器计算下一个工作日LocalDateTime with2 now.with(e - {LocalDateTime e1 (LocalDateTime) e;DayOfWeek dayOfWeek e1.getDayOfWeek();if (dayOfWeek.equals(DayOfWeek.FRIDAY)) {return e1.plusDays(3);} else if (dayOfWeek.equals(DayOfWeek.SATURDAY)) {return e1.plusDays(2);} else {return e1.plusDays(1);}});System.out.println(with2);
2.4 DateTimeFormatter日期格式化三种格式化方法预定义的标准格式语言环境相关的格式自定义的格式2.4.1 预定义的标准格式JDK提供的格式化格式LocalDate localDate LocalDate.now();
String format localDate.format(DateTimeFormatter.ISO_DATE);
输出2020-04-03
2.4.1 自定义的时间格式//自定义日期格式化方式可以通过format和parse对日期进行格式化
DateTimeFormatter dateTimeFormatter DateTimeFormatter.ofPattern(yyyy年MM月dd日);
String format1 localDate.format(dateTimeFormatter);
System.out.println(format1);
localDate.parse(format1,dateTimeFormatter);输出2020年04月03日2020-04-03
2.5 时区处理2.5.1 ZoneId获取所有的时区信息 SetString availableZoneIds ZoneId.getAvailableZoneIds();
获取指定时区信息的ZoneId对象ZoneId of ZoneId.of(Asia/Chungking);
2.5.2 ZonedDateTime获取一个带时区的日期时间对象ZonedDateTime now ZonedDateTime.now();System.out.println(now);
//输出
2020-04-03T14:22:54.25008:00[Asia/Shanghai]
其他用法和LocalDateTime类相同7000字长文带你深入IOC加载流程mp.weixin.qq.comSpringMVC的执行流程你想知道的都有mp.weixin.qq.com