Explorar o código

更新 '_posts/joda-vs-javatime.md'

aaronwei %!s(int64=5) %!d(string=hai) anos
pai
achega
8f36ea94fd
Modificáronse 1 ficheiros con 23 adicións e 15 borrados
  1. 23 15
      _posts/joda-vs-javatime.md

+ 23 - 15
_posts/joda-vs-javatime.md

@@ -12,39 +12,47 @@ Joda time 弥补了 Java 在这方面的不足,但是在 Java8 时,增加了
 
 Joda Date 的核心概念,这些概念在 java time 中基本也能找到对应:
 
+### instant
+
 表示一个时刻,使用从 1970-01-01 00:00:00 至今的毫秒数表示
 
 Joda time:
 
 ```
-DateTime dt = <span class="hljs-keyword">new</span> DateTime();
+DateTime dt = new DateTime();
 Instant instant = dt.toInstant();
-<span class="copy-code-btn">&#x590D;&#x5236;&#x4EE3;&#x7801;</span>
 ```
 
 Java time:
 
+```
+Clock clock = Clock.systemDefaultZone();
+Instant instant = clock.instant();
+```
+
+### interval 
+
 表示两个 instant 之间的间隔,左闭右开。
 
 Joda time:
 
 ```
-DateTime dt = <span class="hljs-keyword">new</span> DateTime();
-DateTime dt1 = <span class="hljs-keyword">new</span> DateTime();
-Interval interval = <span class="hljs-keyword">new</span> Interval(dt.toInstant(), dt1.toInstant());
-<span class="copy-code-btn">&#x590D;&#x5236;&#x4EE3;&#x7801;</span>
+DateTime dt = new DateTime();
+DateTime dt1 = new DateTime();
+Interval interval = new Interval(dt.toInstant(), dt1.toInstant());
 ```
 
 java time 中没有提供类似的 API,因为 JSR-310 标准中没有这个概念。
 
+### duration
+
 用毫秒表示的时间段,通常从 interval 获得 Joda time:
 
 ```
-DateTime dt = <span class="hljs-keyword">new</span> DateTime();
-DateTime dt1 = <span class="hljs-keyword">new</span> DateTime();
-Interval interval = <span class="hljs-keyword">new</span> Interval(dt.toInstant(), dt1.toInstant());
+DateTime dt = new DateTime();
+DateTime dt1 = new DateTime();
+Interval interval = new Interval(dt.toInstant(), dt1.toInstant());
 Duration duration = interval.toInstant();
-<span class="copy-code-btn">&#x590D;&#x5236;&#x4EE3;&#x7801;</span>
 ```
 
 Java time:
@@ -53,18 +61,19 @@ Java time:
 LocalDateTime l1 = LocalDateTime.now();
 LocalDateTime l2 = LocalDateTime.now();
 Period period = Period.between(l1.toLocalDate(), l2.toLocalDate());
-<span class="copy-code-btn">&#x590D;&#x5236;&#x4EE3;&#x7801;</span>
+
 ```
 
+### period
+
 同样表示时间段,比如 3年,5个月,而 duration 使用毫秒表示
 
 Joda time:
 
 ```
-DateTime dt1 = <span class="hljs-keyword">new</span> DateTime();
-DateTime dt2 = <span class="hljs-keyword">new</span> DateTime();
+DateTime dt1 = new DateTime();
+DateTime dt2 = new DateTime();
 Period period = Period.fieldDifference(dt1.toLocalDateTime(), dt2.toLocalDateTime());
-<span class="copy-code-btn">&#x590D;&#x5236;&#x4EE3;&#x7801;</span>
 ```
 
 Java time:
@@ -73,7 +82,6 @@ Java time:
 LocalDateTime l1 = LocalDateTime.now();
 LocalDateTime l2 = LocalDateTime.now();
 Period period = Period.between(l1.toLocalDate(), l2.toLocalDate());
-<span class="copy-code-btn">&#x590D;&#x5236;&#x4EE3;&#x7801;</span>
 ```
 
 年表,这是 joda-time 设计的基础