成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

寫了這么多年DateUtils,殊不知你還有這么多彎彎繞!

開發 后端
Java 8 推出了新的時間日期類 ZoneId、ZoneOffset、LocalDateTime、ZonedDateTime 和 DateTimeFormatter,處理時區問題更簡單清晰。

大家好,我是哪吒。

在日常開發中,Date工具類使用頻率相對較高,大家通常都會這樣寫:
public static Date getData(String date) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return sdf.parse(date);
}

public static Date getDataByFormat(String date, String format) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    return sdf.parse(date);
}
這很簡單啊,有什么爭議嗎?

你應該聽過“時區”這個名詞,大家也都知道,相同時刻不同時區的時間是不一樣的。

因此在使用時間時,一定要給出時區信息。

public static void getDataByZone(String param, String format) throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(format);

    // 默認時區解析時間表示
    Date date = sdf.parse(param);
    System.out.println(date + ":" + date.getTime());

    // 東京時區解析時間表示
    sdf.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
    Date newYorkDate = sdf.parse(param);
    System.out.println(newYorkDate + ":" + newYorkDate.getTime());
}

public static void main(String[] args) throws ParseException {
   getDataByZone("2023-11-10 10:00:00","yyyy-MM-dd HH:mm:ss");
}

對于當前的上海時區和紐約時區,轉化為 UTC 時間戳是不同的時間。

對于同一個本地時間的表示,不同時區的人解析得到的 UTC 時間一定是不同的,反過來不同的本地時間可能對應同一個 UTC。

格式化后出現的時間錯亂:
public static void getDataByZoneFormat(String param, String format) throws ParseException {
   SimpleDateFormat sdf = new SimpleDateFormat(format);
    Date date = sdf.parse(param);
    // 默認時區格式化輸出
    System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss Z]").format(date));
    // 東京時區格式化輸出
    TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo"));
    System.out.println(new SimpleDateFormat("[yyyy-MM-dd HH:mm:ss Z]").format(date));
}

public static void main(String[] args) throws ParseException {
   getDataByZoneFormat("2023-11-10 10:00:00","yyyy-MM-dd HH:mm:ss");
}

我當前時區的 Offset(時差)是 +8 小時,對于 +9 小時的紐約,整整差了1個小時,北京早上 10 點對應早上東京 11 點。

看看Java 8是如何解決時區問題的:

Java 8 推出了新的時間日期類 ZoneId、ZoneOffset、LocalDateTime、ZonedDateTime 和 DateTimeFormatter,處理時區問題更簡單清晰。

public static void getDataByZoneFormat8(String param, String format) throws ParseException {
    ZoneId zone = ZoneId.of("Asia/Shanghai");
    ZoneId tokyoZone = ZoneId.of("Asia/Tokyo");
    ZoneId timeZone = ZoneOffset.ofHours(2);

    // 格式化器
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
    ZonedDateTime date = ZonedDateTime.of(LocalDateTime.parse(param, dtf), zone);

    // withZone設置時區
    DateTimeFormatter dtfz = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z");
    System.out.println(dtfz.withZone(zone).format(date));
    System.out.println(dtfz.withZone(tokyoZone).format(date));
    System.out.println(dtfz.withZone(timeZone).format(date));
}

public static void main(String[] args) throws ParseException {
    getDataByZoneFormat8("2023-11-10 10:00:00","yyyy-MM-dd HH:mm:ss");
}
  • Asia/Shanghai對應+8,對應2023-11-10 10:00:00。
  • Asia/Tokyo對應+9,對應2023-11-10 11:00:00。
  • timeZone 是+2,所以對應2023-11-10 04:00:00。

在處理帶時區的國際化時間問題,推薦使用jdk8的日期時間類:
  • 通過ZoneId,定義時區;
  • 使用ZonedDateTime保存時間;
  • 通過withZone對DateTimeFormatter設置時區;
  • 進行時間格式化得到本地時間;

思路比較清晰,不容易出錯。

在與前端聯調時,報了個錯,java.lang.NumberFormatException: multiple points,起初我以為是時間格式傳的不對,仔細一看,不對啊。

百度一下,才知道是高并發情況下SimpleDateFormat有線程安全的問題。

下面通過模擬高并發,把這個問題復現一下:

public static void getDataByThread(String param, String format) throws InterruptedException {
    ExecutorService threadPool = Executors.newFixedThreadPool(5);
    SimpleDateFormat sdf = new SimpleDateFormat(format);
    // 模擬并發環境,開啟5個并發線程
    for (int i = 0; i < 5; i++) {
        threadPool.execute(() -> {
            for (int j = 0; j < 2; j++) {
                try {
                    System.out.println(sdf.parse(param));
                } catch (ParseException e) {
                    System.out.println(e);
                }
            }
        });
    }
    threadPool.shutdown();

    threadPool.awaitTermination(1, TimeUnit.HOURS);
}

果不其然,報錯。還將2023年轉換成2220年,我勒個乖乖。

在時間工具類里,時間格式化,我都是這樣弄的啊,沒問題啊,為啥這個不行?原來是因為共用了同一個SimpleDateFormat,在工具類里,一個線程一個SimpleDateFormat,當然沒問題啦!

可以通過TreadLocal 局部變量,解決SimpleDateFormat的線程安全問題。

public static void getDataByThreadLocal(String time, String format) throws InterruptedException {
    ExecutorService threadPool = Executors.newFixedThreadPool(5);

    ThreadLocal<SimpleDateFormat> sdf = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat(format);
        }
    };

    // 模擬并發環境,開啟5個并發線程
    for (int i = 0; i < 5; i++) {
        threadPool.execute(() -> {
            for (int j = 0; j < 2; j++) {
                try {
                    System.out.println(sdf.get().parse(time));
                } catch (ParseException e) {
                    System.out.println(e);
                }
            }
        });
    }
    threadPool.shutdown();

    threadPool.awaitTermination(1, TimeUnit.HOURS);
}

看一下SimpleDateFormat.parse的源碼:
public class SimpleDateFormat extends DateFormat {
 @Override
 public Date parse(String text, ParsePosition pos){
  CalendarBuilder calb = new CalendarBuilder();
  
  Date parsedDate;
  try {
      parsedDate = calb.establish(calendar).getTime();
      // If the year value is ambiguous,
      // then the two-digit year == the default start year
      if (ambiguousYear[0]) {
          if (parsedDate.before(defaultCenturyStart)) {
              parsedDate = calb.addYear(100).establish(calendar).getTime();
          }
      }
  }
 }
}

class CalendarBuilder {
 Calendar establish(Calendar cal) {
     boolean weekDate = isSet(WEEK_YEAR)
                         && field[WEEK_YEAR] > field[YEAR];
     if (weekDate && !cal.isWeekDateSupported()) {
         // Use YEAR instead
         if (!isSet(YEAR)) {
             set(YEAR, field[MAX_FIELD + WEEK_YEAR]);
         }
         weekDate = false;
     }
 
     cal.clear();
     // Set the fields from the min stamp to the max stamp so that
     // the field resolution works in the Calendar.
     for (int stamp = MINIMUM_USER_STAMP; stamp < nextStamp; stamp++) {
         for (int index = 0; index <= maxFieldIndex; index++) {
             if (field[index] == stamp) {
                 cal.set(index, field[MAX_FIELD + index]);
                 break;
             }
         }
     }
  ...
 
 }
}
  • 先new CalendarBuilder()。
  • 通過parsedDate = calb.establish(calendar).getTime();解析時間。
  • establish方法內先cal.clear(),再重新構建cal,整個操作沒有加鎖。

上面幾步就會導致在高并發場景下,線程1正在操作一個Calendar,此時線程2又來了。線程1還沒來得及處理 Calendar 就被線程2清空了。

因此,通過編寫Date工具類,一個線程一個SimpleDateFormat,還是有一定道理的。

責任編輯:姜華 來源: 哪吒編程
相關推薦

2023-11-13 08:49:54

2021-01-14 05:08:44

編譯鏈接

2021-02-03 08:24:32

JavaScript技巧經驗

2017-06-16 16:16:36

庫存扣減查詢

2018-10-06 21:51:37

代碼SOLID編程

2017-07-04 14:01:40

機房機柜

2018-10-07 06:30:40

代碼設計模式面向對象原則

2018-06-26 15:00:24

Docker安全風險

2022-05-29 08:54:44

Edge瀏覽器

2022-07-26 23:43:29

編程語言開發Java

2017-12-21 19:38:50

潤乾中間表

2015-03-27 10:20:41

谷歌地圖谷歌偉大

2022-03-03 07:00:43

Mybatiswhere標簽

2017-11-30 07:30:27

程序員代碼軟件世界觀

2018-05-29 14:57:59

HashMap容量初始化

2024-05-13 16:22:25

固態硬盤接口硬盤

2024-01-31 12:34:16

panic錯誤檢測recover

2024-01-02 12:48:49

2021-05-21 05:24:03

Excel數據技巧

2013-01-15 09:41:45

編程語言
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日本一区精品 | 国产传媒视频在线观看 | 久久精品1 | 国内精品一区二区三区 | 免费精品视频 | 91精品国产91久久综合桃花 | 亚洲视频免费在线观看 | 天堂综合网 | 国产精成人 | 日本高清视频网站 | 日日草天天干 | 久久精品成人 | 日韩欧美网 | 久久鲁视频| 成人在线一区二区三区 | 久久99精品视频 | av一级一片 | 久久夜视频 | 6996成人影院网在线播放 | 免费a国产 | 中文字幕 在线观看 | 欧美精品在线一区 | 欧美日在线 | 久久草在线视频 | 亚洲 精品 综合 精品 自拍 | 性高湖久久久久久久久3小时 | 欧美一区视频在线 | 亚洲狠狠爱 | 国产精品国产三级国产aⅴ无密码 | 男人的天堂在线视频 | 中文字幕亚洲视频 | www.亚洲一区| 高清国产午夜精品久久久久久 | 97国产精品视频人人做人人爱 | 亚洲精品国产综合区久久久久久久 | 免费看片在线播放 | 亚洲一区精品在线 | 香蕉av免费 | 91色啪 | 久久91av | 国产精品精品视频一区二区三区 |