日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

日期格式化为yyyymmdd_你还在用SimpleDateFormat格式化时间嘛

發布時間:2025/3/12 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日期格式化为yyyymmdd_你还在用SimpleDateFormat格式化时间嘛 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Jdk1.8之時間處理

該文章已經同步到Github:https://github.com/stackInk/makerstack

1. 傳統時間處理的問題

1.1 多線程環境下的SimpleDateFormat

當多個線程使用同一個時間處理對象進行對日期的格式化的時候,會出現java.lang.NumberFormatException: multiple points。主要原因是由于SimpleDateFormat是線程不安全的,當線程共享的時候,會引發這個異常。

1.1.1 代碼演示

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd"); //線程池中線程共享了SimpleDateFormat,引發線程不安全 Callable<String> callable = () -> simpleDateFormat.parse("20200402").toString();ExecutorService executorService = Executors.newFixedThreadPool(10); List<Future<String>> list = new LinkedList<>();for (int i = 0; i < 10; i++) {Future<String> submit = executorService.submit(callable);list.add(submit); } for (Future<String> stringFuture : list) {String s = stringFuture.get();System.out.println(s); } executorService.shutdown();

解決方法:

  • 線程不共享變量SimpleDateFormat,每一個線程在進行日期格式化的時候都自己創建一個
ExecutorService executorService = Executors.newFixedThreadPool(10); List<Future<String>> list = new LinkedList<>(); for (int i = 0; i < 10; i++) {Future<String> submit = executorService.submit(new MyCallable01("20200403"));list.add(submit); } for (Future<String> stringFuture : list) {String s = stringFuture.get();System.out.println(s);} executorService.shutdown();class MyCallable01 implements Callable<String>{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為每一個線程綁定一個SimpleDateFormate
Future<String> submit = executorService.submit(() -> ResolveByThreadLocal.converDateStrToDate("20200405"));public class ResolveByThreadLocal {//創建一個綁定每一個變量的ThreadLocalprivate static final ThreadLocal<SimpleDateFormat> 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市區偏移量計算
    • ZonedDateTime
  • java.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

  • 獲取所有的時區信息
Set<String> 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.250+08:00[Asia/Shanghai]

其他用法和LocalDateTime類相同

7000字長文帶你深入IOC加載流程?mp.weixin.qq.comSpringMVC的執行流程,你想知道的都有?mp.weixin.qq.com

總結

以上是生活随笔為你收集整理的日期格式化为yyyymmdd_你还在用SimpleDateFormat格式化时间嘛的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。