Table of Contents
ToggleJava OffsetTime Class
儲存時間,精確到奈秒,以及帶有偏移量的時間,提供增加、減少、比較時間、或兩個時間差值的方法, OffsetTime Class 介紹常見的 plus 、 minus 、 isBefore 、 isAfter 、 until 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- offsettime
| +- OffsetTimeClassTest.java
單元測試
OffsetTime Class Java 提供時間加減、比較前後等操作 OffsetTime 。
plus
OffsetTime Class Java 建立一個 OffsetTime ,提供指定時間加法運算。
@Test
public void plus() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.plusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(7, hour);
result = offsetTime.plusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(2, minute);
result = offsetTime.plusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(5, second);
}
7
2
5
plusTemporalAmount
OffsetTime Class Java 建立一個 OffsetTime ,使用 Period 、 Duration 提供指定時間加法運算。
@Test
public void plusTemporalAmount() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.plus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(7, hour);
result = offsetTime.plus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(2, minute);
result = offsetTime.plus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(5, second);
}
7
2
5
minus
OffsetTime Class Java 建立一個 OffsetTime ,提供指定時間減法運算。
@Test
public void minus() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.minusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(5, hour);
result = offsetTime.minusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(0, minute);
result = offsetTime.minusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(3, second);
}
5
0
3
minusTemporalAmount
OffsetTime Class Java 建立一個 OffsetTime ,使用 Period 、 Duration 提供指定時間減法運算。
@Test
public void minusTemporalAmount() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.minus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(5, hour);
result = offsetTime.minus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(0, minute);
result = offsetTime.minus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(3, second);
}
5
0
3
isBefore
OffsetTime Class in Java 建立兩個 OffsetTime ,時間比對前後。
@Test
public void isBefore() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime offsetTime2 = OffsetTime.of(7, 1, 4, 0, offset);
boolean result = offsetTime.isBefore(offsetTime2);
System.out.println(result);
assertTrue(result);
result = offsetTime2.isBefore(offsetTime);
System.out.println(result);
assertFalse(result);
}
true
false
isAfter
OffsetTime Class in Java 建立兩個 OffsetTime ,時間比對前後, Java OffsetTime Class Example 提供範例參考。
@Test
public void isAfter() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime offsetTime2 = OffsetTime.of(5, 0, 3, 0, offset);
boolean result = offsetTime.isAfter(offsetTime2);
System.out.println(result);
assertTrue(result);
result = offsetTime2.isAfter(offsetTime);
System.out.println(result);
assertFalse(result);
}
true
false
until
OffsetTime Class in Java 建立兩個 OffsetTime ,取得時間的差值, Java OffsetTime Class Example 提供範例參考。
@Test
public void until() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime offsetTime2 = OffsetTime.of(7, 1, 4, 0, offset);
long result = offsetTime.until(offsetTime2, ChronoUnit.HOURS);
System.out.println(result);
assertEquals(1, result);
result = offsetTime.until(offsetTime2, ChronoUnit.MINUTES);
System.out.println(result);
assertEquals(60, result);
result = offsetTime.until(offsetTime2, ChronoUnit.SECONDS);
System.out.println(result);
assertEquals(3600, result);
}
1
60
3600
OffsetTimeClassTest.java
OffsetTime Class in Java 新增單元測試,驗證 Java OffsetTime Class Example 是否符合預期。
package org.ruoxue.java_147.datetime.offsettime;
import static org.junit.Assert.*;
import java.time.Duration;
import java.time.OffsetTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import org.junit.Test;
public class OffsetTimeClassTest {
@Test
public void plus() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.plusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(7, hour);
result = offsetTime.plusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(2, minute);
result = offsetTime.plusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(5, second);
}
@Test
public void plusTemporalAmount() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.plus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(7, hour);
result = offsetTime.plus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(2, minute);
result = offsetTime.plus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(5, second);
}
@Test
public void minus() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.minusHours(1);
int hour = result.getHour();
System.out.println(hour);
assertEquals(5, hour);
result = offsetTime.minusMinutes(1);
int minute = result.getMinute();
System.out.println(minute);
assertEquals(0, minute);
result = offsetTime.minusSeconds(1);
int second = result.getSecond();
System.out.println(second);
assertEquals(3, second);
}
@Test
public void minusTemporalAmount() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime result = offsetTime.minus(Duration.ofHours(1));
int hour = result.getHour();
System.out.println(hour);
assertEquals(5, hour);
result = offsetTime.minus(Duration.ofMinutes(1));
int minute = result.getMinute();
System.out.println(minute);
assertEquals(0, minute);
result = offsetTime.minus(Duration.ofSeconds(1));
int second = result.getSecond();
System.out.println(second);
assertEquals(3, second);
}
@Test
public void isBefore() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime offsetTime2 = OffsetTime.of(7, 1, 4, 0, offset);
boolean result = offsetTime.isBefore(offsetTime2);
System.out.println(result);
assertTrue(result);
result = offsetTime2.isBefore(offsetTime);
System.out.println(result);
assertFalse(result);
}
@Test
public void isAfter() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime offsetTime2 = OffsetTime.of(5, 0, 3, 0, offset);
boolean result = offsetTime.isAfter(offsetTime2);
System.out.println(result);
assertTrue(result);
result = offsetTime2.isAfter(offsetTime);
System.out.println(result);
assertFalse(result);
}
@Test
public void until() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
OffsetTime offsetTime2 = OffsetTime.of(7, 1, 4, 0, offset);
long result = offsetTime.until(offsetTime2, ChronoUnit.HOURS);
System.out.println(result);
assertEquals(1, result);
result = offsetTime.until(offsetTime2, ChronoUnit.MINUTES);
System.out.println(result);
assertEquals(60, result);
result = offsetTime.until(offsetTime2, ChronoUnit.SECONDS);
System.out.println(result);
assertEquals(3600, result);
}
}
心得分享
Java OffsetTime Class Example 帶有偏移量的時間,物件的值不會被改變,可以同時在不同的執行緒下被使用,儲存時間,精確到奈秒以及與 UTC / 格林威治的偏移量, OffsetTime Class in Java 提供 plus 、 minus 、 isBefore 、 isAfter 等操作範例。