<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Java 8 日期時間 API > 原文: [https://javatutorial.net/java-8-date-time-api](https://javatutorial.net/java-8-date-time-api) Java 8 引入了新的日期時間 API,其目的是彌補舊的日期時間 API 的缺點。 ![java-featured-image](https://img.kancloud.cn/05/3e/053ee0bb59842d92359246c98f815e0c_780x330.jpg) 以前的日期時間 api 不是線程安全的,新日期時間 api 的替代品是它沒有任何設置方法。 新 API 修復的另一個缺點是設計不佳。 舊的 API 具有較少直接的日期操作方法。 而且舊 API 的另一個缺點是程序員必須編寫大量代碼來處理時區問題。 新的 API 不僅解決了所有這些問題,而且還引入了`java.time`包中的 2 個重要類: * 本地 – 時區處理沒有復雜度 * 時區 – 處理各種時區的更復雜的日期時間 API **本地時間 API** 在不需要時區時應使用 **使用主要方法調用**的本地日期時間 API 的示例 ```java import java.time.*; import java.time.format.DateTimeFormatter; public class Main { public static void LocalDateTimeAPI() { LocalDate date = LocalDate.now(); LocalTime time = LocalTime.now(); LocalDateTime now = LocalDateTime.now(); DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); String formatedTime = now.format(format); Month month = now.getMonth(); int day = now.getDayOfMonth(); int seconds = now.getSecond(); System.out.println("Current date: " + date); System.out.println("Current time: " + time); System.out.println("Current date and time: " + now); System.out.println("in foramatted manner " + formatedTime); System.out.println("Month: " + month + "\nDay: " + day + "\nSeconds: " + seconds); } public static void main(String[] args) { LocalDateTimeAPI(); } } ``` **輸出**: ```java Current date: 2019-07-20 Current time: 14:10:58.492 Current date and time: 2019-07-20T14:10:58.492 in foramatted manner 20-07-2019 14:10:58 Month: JULY Day: 20 Seconds: 58 ``` **時區日期時間 API 示例** ```java import java.time.ZonedDateTime; import java.time.ZoneId; public class ZonedTime { public void testZonedDateTime() { ZoneId zone = ZoneId.of("Europe/Paris"); System.out.println("ZoneId: " + zone); ZoneId currentZone = ZoneId.systemDefault(); System.out.println("CurrentZone: " + currentZone); } public static void main(String args[]) { ZonedTime zonedTimeExample = new ZonedTime(); zonedTimeExample.testZonedDateTime(); } } ``` **輸出** ```java ZoneId: Europe/Paris CurrentZone: Europe/London ``` 如您所見,時區日期時間 API 可以讓您訪問特定時區,還可以為您提供時區或系統的默認時區。 **計時單位示例** ```java import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class ChronoUnits { public void chromoUnits() { LocalDate today = LocalDate.now(); System.out.println("Current date: " + today); LocalDate week = today.plus(1, ChronoUnit.WEEKS); System.out.println("1 week from now: " + week); LocalDate month = today.plus(1, ChronoUnit.MONTHS); System.out.println("1 month from now: " + month); LocalDate year = today.plus(1, ChronoUnit.YEARS); System.out.println("1 year from now: " + year); LocalDate decade = today.plus(1, ChronoUnit.DECADES); System.out.println("1 decade from now: " + decade); } public static void main(String args[]) { ChronoUnits ChronoUnitsExample = new ChronoUnits(); ChronoUnitsExample.chromoUnits(); } } ``` **輸出**: ```java Current date: 2019-07-20 1 week from now: 2019-07-27 1 month from now: 2019-08-20 1 year from now: 2020-07-20 1 decade from now: 2029-07-20 ``` **周期和持續時間** 周期處理基于日期的時間,而持續時間處理基于時間的時間。 ```java import java.time.temporal.ChronoUnit; import java.time.LocalDate; import java.time.LocalTime; import java.time.Duration; import java.time.Period; public class PeriodDuration { public void testDuration() { LocalTime currentTime = LocalTime.now(); Duration offtime5 = Duration.ofHours(5); LocalTime timeOff5Hours = currentTime.plus(offtime5); Duration duration = Duration.between(currentTime, timeOff5Hours); System.out.println("Duration: " + duration); } public void testPeriod() { LocalDate currentDate = LocalDate.now(); System.out.println("Current date: " + currentDate); LocalDate nextMonth = currentDate.plus(1, ChronoUnit.MONTHS); System.out.println("Next month: " + nextMonth); Period timePeriod = Period.between(nextMonth, currentDate); System.out.println("Period: " + timePeriod); } public static void main(String args[]) { PeriodDuration periodDuration = new PeriodDuration(); periodDuration.testPeriod(); periodDuration.testDuration(); } } ``` **輸出** ```java Current date: 2019-07-20 Next month: 2019-08-20 Period: P-1M Duration: PT-19H ```
                  <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>

                              哎呀哎呀视频在线观看