<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                >[success] # java.time 1. `java.time` 是Java8日期類,JDK 1.0中包含了一個`java.util.Date`類,但是它的大多數方法已經在`JDK 1.1引入Calendar類`之后**被棄用**,但是Calendar 類也相對存在一些問題 1.1. 格式化只對Date類有用,對Calendar類則不能使用 1.2. Date類中的年份是從1900開始的,而月份都從0開始 需要去減掉1900 1.3. 非線程安全 >[info] ## java8 日期類型 **Java 8通過發布新的Date-Time API來進一步加強對 日期與時間的處理**。 1. `java.time`包:該包日期/時間API的基礎包。 2. `java.time.chrono`包:該包提供對不同日歷系統的訪問。 3. `java.time.format`包:該包能夠格式化和解析日期時間對象。 4. `java.time.temporal`包:該包包含底層框架和擴展特性。 5. `java.time.zone`包:該包支持不同時區以及相關規則的類。 >[danger] ##### LocalDate -- 類 1. `java.time.LocalDate`類主要用于描述年\-月\-日格式的日期信息,該類不表示時間和時區信息 | 方法聲明 |功能介紹| | -- |--| | static LocalDate now() |在默認時區中從系統時鐘獲取當前日期| ~~~ public class DateTest { public static void main(String[] args) { // 獲取當前日期信息并打印 LocalDate ld = LocalDate.now(); System.out.println(ld); // 2022-10-06 } } ~~~ >[danger] ##### LocalTime -- 類 1. `java.time.LocalTime` 類主要用于描述時間信息,**可以描述時分秒以及納秒** |方法聲明 |功能介紹| | -- |--| |static LocalTime now() |從默認時區的系統時間中獲取當前時間| |static LocalTime now(ZoneId zone) |獲取指定時區的當前時間| ~~~ import java.time.LocalTime; public class DateTest { public static void main(String[] args) { // 獲取當前時間信息并打印 LocalTime now1 = LocalTime.now(); System.out.println("獲取到的當前時間是:" + now1); // 獲取到的當前時間是:15:53:25.889174300 } } ~~~ >[danger] ##### LocalDateTime -- 類 1. `java.time.LocalDateTime`類主要用于描述ISO-8601日歷系統中沒有時區的日期時間 |方法聲明 |功能介紹| | -- |--| |static LocalDateTime now()|從默認時區的系統時間中獲取當前日期時間| |static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)|根據參數指定的年月日時分秒信息來設置日期時間| |int getYear() |獲取年份字段的數值| |int getMonthValue() |獲取1到12之間的月份字段| |int getDayOfMonth()|獲取日期字段| |int getHour() |獲取小時數| |int getMinute() |獲取分鐘數| |int getSecond()| 獲取秒數| |LocalDateTime withYear(int year)| 設置為參數指定的年| |LocalDateTime withMonth(int month) |設置為參數指定的月| |LocalDateTime withDayOfMonth(int dayOfMonth) |設置為參數指定的日| |LocalDateTime withHour(int hour) |設置為參數指定的時| |LocalDateTime withMinute(int minute) |設置為參數指定的分| |LocalDateTime withSecond(int second) |設置為參數指定的秒| |LocalDateTime plusYears(long years) |加上參數指定的年| |LocalDateTime plusMonths(long months) |加上參數指定的月| |LocalDateTime plusDays(long days) |加上參數指定的日| |LocalDateTime plusHours(long hours) |加上參數指定的時| |LocalDateTime plusMinutes(long minutes) |加上參數指定的分| |LocalDateTime plusSeconds(long seconds) |加上參數指定的秒| |LocalDateTime minusYears(long years) |減去參數指定的年| |LocalDateTime minusMonths(long months) |減去參數指定的月| |LocalDateTime minusDays(long days) |減去參數指定的日| |LocalDateTime minusHours(long hours) |減去參數指定的時| |LocalDateTime minusMinutes(long minutes) |減去參數指定的分| |LocalDateTime minusSeconds(long seconds) |減去參數指定的秒| ~~~ import java.time.LocalDateTime; public class DateTest { public static void main(String[] args) { // 獲取當前日期時間信息并打印,使用最多 LocalDateTime now2 = LocalDateTime.now(); System.out.println("獲取到的當前日期時間是:" + now2); // 獲取到的當前日期時間是:2022-10-06T15:54:13.178964500 // 設置指定年月日時分秒 LocalDateTime of = LocalDateTime.of(2022, 8, 8, 8, 8, 8); // 自動調用toString方法 System.out.println(of); // 2022-08-08T08:08:08 System.out.println("獲取到的年是:" + of.getYear()); // 2022 System.out.println("獲取到的月是:" + of.getMonthValue()); // 8 System.out.println("獲取到的日是:" + of.getDayOfMonth()); // 8 System.out.println("獲取到的時是:" + of.getHour()); // 8 System.out.println("獲取到的分是:" + of.getMinute()); // 8 System.out.println("獲取到的秒是:" + of.getSecond()); // 8 // ----------------------設置指定年月日 ------------------ LocalDateTime of1 = LocalDateTime.of(2022, 8, 8, 8, 8, 8); // 調用對象本身的數據內容不會改變(并沒有改變of1)。 創建了一個新的對象,由此證明了不可變性 LocalDateTime localDateTime = of1.withYear(2012); System.out.println("localDateTime = " + localDateTime); // 2012-08-08T20:08:08 System.out.println("of1 = " + of1); // of1 = 2022-08-08T08:08:08 LocalDateTime localDateTime1 = localDateTime.withMonth(12); System.out.println("localDateTime1 = " + localDateTime1); // localDateTime1 = 2012-12-08T08:08:08 // 日期的加減 LocalDateTime now3 = LocalDateTime.now(); // 2022-10-11T16:14:00.115520 LocalDateTime day5 = now3.plusDays(5); System.out.println(day5); // 2022-10-11T16:13:14.738725600 LocalDateTime day10 = now3.minusDays(10); System.out.println(day10); // 2022-09-26T16:14:00.115520 } } ~~~ >[danger] ##### Instant --- 類 1. `java.time.Instant`類主要用于描述瞬間的時間點信息 |方法聲明 |功能介紹| | -- |--| |static Instant now()| 從系統時鐘上獲取當前時間| |OffsetDateTime atOffset(ZoneOffset offset) |將此瞬間與偏移量組合以創建偏移日期時間| | static Instant ofEpochMilli(long epochMilli)|根據參數指定的毫秒數來構造對象,參數為距離1970年1月1 日0時0分0秒的毫秒數| | long toEpochMilli()| 獲取距離1970年1月1日0時0分0秒的毫秒數| ~~~ import java.time.Instant; import java.time.OffsetDateTime; import java.time.ZoneOffset; public class DateTest { public static void main(String[] args) { // 1.使用Instant類來獲取當前系統時間 并不是當前系統的默認時區 本初子午線 差8小時 東八區 Instant now = Instant.now(); System.out.println("獲取到的當前時間為:" + now); // 獲取到的當前時間為:2022-10-06T09:15:47.127236Z // 2.加上時區所差的8個小時 OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8)); System.out.println("偏移后的日期時間為:" + offsetDateTime); // 偏移后的日期時間為:2022-10-06T17:15:47.127236+08:00 System.out.println("--------------------------------------------------------"); // 3.獲取當前調用對象距離標準基準時間的毫秒數 long g1 = now.toEpochMilli(); System.out.println("獲取到的毫秒差為:" + g1); // 獲取到的毫秒差為:1665047747127 // 4.根據參數指定的毫秒數來構造對象 Instant instant = Instant.ofEpochMilli(g1); System.out.println("根據參數指定的毫秒數構造出來的對象為:" + instant); // 偏移后的日期時間為:2022-10-06T17:15:47.127236+08:00 } } e ~~~ >[danger] ##### DateTimeFormatter -- 類 1. `java.time.format.DateTimeFormatter`類主要用于格式化和解析日期 |方法聲明 |功能介紹| | -- |--| |static DateTimeFormatter ofPattern(String pattern) |根據參數指定的模式來獲取對象| |String format(TemporalAccessor temporal) |將參數指定日期時間轉換為字符串| |TemporalAccessor parse(CharSequence text) |將參數指定字符串轉換為日期時間| ~~~ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; public class DateTest { public static void main(String[] args) { // 1.獲取當前系統的日期時間并打印 LocalDateTime now = LocalDateTime.now(); System.out.println("now = " + now); // now = 2022-10-06T19:53:47.821575900 // 2.按照指定的格式準備一個DateTimeFormatter類型的對象 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 3.實現日期時間向字符串類型的轉換并打印 String str = dateTimeFormatter.format(now); System.out.println("調整格式后的結果是:" + str); // 調整格式后的結果是:2022-10-06 19:53:47 // 4.實現字符串類型到日期時間類型的轉換并打印 TemporalAccessor parse = dateTimeFormatter.parse(str); System.out.println("轉回去的結果是:" + parse); // 轉回去的結果是:{},ISO resolved to 2022-10-06T19:53:47 } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看