<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          請別再使用 SimpleDateFormat 格式化時(shí)間了,DateTimeFormatter 更出色!

          共 5395字,需瀏覽 11分鐘

           ·

          2022-07-31 15:11

          程序員的成長之路
          互聯(lián)網(wǎng)/程序員/技術(shù)/資料共享 
          關(guān)注


          閱讀本文大概需要 3.5 分鐘。

          來自:blog.csdn.net/Window_mouse/article/

          DateTimeFormatter類

          我們先來看看SImpleDateFormat類的部分源碼,如圖1所示。
          圖1
          接著再來看看DateTimeFormatter類的部分源碼,如 圖2所示。
          圖2
          由上可知,與SimpleDateFormat不同的是,DateTimeFormatter不但是不變對象,它還是線程安全的。線程的概念我們會(huì)在后面涉及到。
          現(xiàn)在我們只需要記?。阂?yàn)?code style="margin: 0px 2px;padding: 2px 4px;outline: 0px;max-width: 100%;box-sizing: border-box !important;overflow-wrap: break-word;font-size: 14px;border-radius: 4px;background-color: rgba(27, 31, 35, 0.05);font-family: "Operator Mono", Consolas, Monaco, Menlo, monospace;word-break: break-all;color: rgb(60, 112, 198);visibility: visible;">SimpleDateFormat不是線程安全的,使用的時(shí)候,只能在方法內(nèi)部創(chuàng)建新的局部變量。而DateTimeFormatter可以只創(chuàng)建一個(gè)實(shí)例,到處引用。
          接下來,我們來說一說DateTimeFormatter類的常用方法
          //創(chuàng)建一個(gè)格式化程序使用指定的模式
          static DateTimeFormatter ofPattern(String pattern) 
            
          //創(chuàng)建一個(gè)格式化程序使用指定的模式和現(xiàn)場。
          static DateTimeFormatter ofPattern(String pattern, Locale locale) 
           
          //使用此格式化程序格式的日期時(shí)間對象
          String format(TemporalAccessor temporal) 
          其中,TemporalAccessor是一個(gè)接口,其實(shí)現(xiàn)類有LocalDate、LocalTime、LocalDateTime、ZonedDateTime等……
          所以我們在使用format方法時(shí),一般傳入其實(shí)現(xiàn)類的實(shí)例化對象即可。
          接下來我們舉幾個(gè)例子。

          范例1:創(chuàng)建DateTimeFormatter

          package edu.blog.test07;

          import java.time.LocalDateTime;
          import java.time.format.DateTimeFormatter;

          public class DateTimeFormatterTestDemo01 {
              public static void main(String[] args) {
                  //自定義輸出格式
                  DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
                  System.out.println(dtf.format(LocalDateTime.now()));
                  System.out.println("===================================");
                  //自定義格式解析
                  LocalDateTime localDateTime = LocalDateTime.parse("2001/07/27 22:22:22", dtf);
                  System.out.println(localDateTime);
              }
          }

          /*
          結(jié)果:
          2021/04/02 23:14:46
          ===================================
          2001-07-27T22:22:22
          */

          由上可知,DateTimeFormatter類格式化字符串的使用方式與SImpleDateFormat一樣。
          此外,另一種創(chuàng)建DateTimeFormatter的方法是,傳入格式化字符串的同時(shí),同時(shí)指定Locale。

          范例2:按照Locale默認(rèn)習(xí)慣格式化

          package edu.blog.test07;

          import java.time.ZonedDateTime;
          import java.time.format.DateTimeFormatter;
          import java.util.Locale;

          public class DateTimeFormatterTestDemo02 {
              public static void main(String[] args) {
                  ZonedDateTime zonedDateTime = ZonedDateTime.now();
                  System.out.println(zonedDateTime);
                  System.out.println("==============================");

                  DateTimeFormatter formatter01 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ZZZZ");
                  System.out.println(formatter01.format(zonedDateTime));
                  System.out.println("==============================");

                  DateTimeFormatter formatter02 = DateTimeFormatter.ofPattern("yyyy MMM dd EE:HH:mm", Locale.CHINA);
                  System.out.println(formatter02.format(zonedDateTime));
                  System.out.println("==============================");

                  DateTimeFormatter formatter03 = DateTimeFormatter.ofPattern("E, MMMM/dd/yyyy HH:mm", Locale.US);
                  System.out.println(formatter03.format(zonedDateTime));
              }
          }

          /*
          結(jié)果:
          2021-04-02T23:27:59.326+08:00[Asia/Shanghai]
          ==============================
          2021-04-02T23:27:GMT+08:00
          ==============================
          2021 四月 02 星期五:23:27
          ==============================
          Fri, April/02/2021 23:27
          */

          運(yùn)行本程序,分別以默認(rèn)方式、中國地區(qū)和美國地區(qū)對當(dāng)前時(shí)間進(jìn)行顯示,結(jié)果如上所述。
          在格式化字符串中,如果需要輸出固定字符,可以用’xxx’表示。
          當(dāng)我們直接調(diào)用"System.out.println()"對一個(gè)ZonedDateTime或者LocalDateTime實(shí)例進(jìn)行打印的時(shí)候,實(shí)際上,調(diào)用的是它們的toString()方法,默認(rèn)的toString()方法顯示的字符串就是按照ISO 8601格式顯示的,我們可以通過DateTimeFormatter預(yù)定義的幾個(gè)靜態(tài)變量來引用。

          范例3:過DateTimeFormatter預(yù)定義靜態(tài)變量

          package edu.blog.test07;

          import java.time.LocalDateTime;
          import java.time.format.DateTimeFormatter;

          public class DateTimeFormatterTestDemo03 {
              public static void main(String[] args) {
                  LocalDateTime localDateTime = LocalDateTime.now();
                  System.out.println(localDateTime);
                  System.out.println(DateTimeFormatter.ISO_DATE.format(localDateTime));
                  System.out.println(DateTimeFormatter.ISO_DATE_TIME.format(localDateTime));
              }
          }

          /*
          結(jié)果:
          2021-04-02T23:38:11.707
          2021-04-02
          2021-04-02T23:38:11.707
          */

          總結(jié)

          ZonedDateTimeLocalDateTime進(jìn)行格式化,需要使用DateTimeFormatter類,DateTimeFormatter可以通過格式化字符串和Locale對日期和時(shí)間進(jìn)行定制輸出。
          <END>

          推薦閱讀:

          “師媛”再惹爭議!上海女老師上課時(shí)座無虛席,本應(yīng)是好事,可網(wǎng)上卻罵聲一片!

          面試官:Java8 lambda 表達(dá)式 forEach 如何提前終止?

          互聯(lián)網(wǎng)初中高級(jí)大廠面試題(9個(gè)G)

          內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!

          ?戳閱讀原文領(lǐng)??!                                  朕已閱 

          瀏覽 30
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  北条麻妃无码一区二区三区视频 | 白峰美羽无码在线观看 | 免费A片观看 | 日日躁夜夜躁狠狠躁av麻豆 | 直接看黄色电影 |