Table of Contents
ToggleInitialize OffsetTime in Java
初始化 OffsetTime 具有偏移量的時間的不可變物件,可以同時在不同的執行緒下被使用,介紹常見的 of 、 parse 、 from 等方法,了解 Initialize OffsetTime Java 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- datetime
| +- offsettime
| +- InitializeOffsetTimeTest.java
單元測試
Java Initialize OffsetTime 提供初始化操作 OffsetTime 。
now
建立一個 OffsetTime ,提供目前時間。
@Test
public void now() {
OffsetTime offsetTime = OffsetTime.now();
System.out.println(offsetTime);
ZoneId zone = ZoneId.of("Brazil/Acre");
offsetTime = OffsetTime.now(zone);
System.out.println(offsetTime);
zone = ZoneId.of("UTC-5");
Clock clock = Clock.system(zone);
offsetTime = OffsetTime.now(clock);
System.out.println(offsetTime);
zone = ZoneId.of("Etc/GMT+5");
offsetTime = OffsetTime.now(zone);
System.out.println(offsetTime);
}
19:32:59.368+08:00
06:32:59.368-05:00
06:32:59.368-05:00
06:32:59.368-05:00
of
建立一個 OffsetTime ,建立指定時間。
@Test
public void of() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
System.out.println(offsetTime);
assertEquals("06:01:04-05:00", offsetTime.toString());
offsetTime = OffsetTime.of(LocalTime.of(6, 1, 4, 0), offset);
System.out.println(offsetTime);
assertEquals("06:01:04-05:00", offsetTime.toString());
}
06:01:04-05:00
06:01:04-05:00
parse
Java Initialize OffsetTime 建立一個 OffsetTime ,建立指定時間。
@Test
public void parse() {
OffsetTime offsetTime = OffsetTime.parse("06:01:04-05:00");
System.out.println(offsetTime);
assertEquals("06:01:04-05:00", offsetTime.toString());
offsetTime = OffsetTime.parse("06:01:04.123456789-05:00");
System.out.println(offsetTime);
assertEquals("06:01:04.123456789-05:00", offsetTime.toString());
}
06:01:04-05:00
06:01:04.123456789-05:00
from
OffsetTime Initialization Java 建立一個 OffsetTime ,從另一個時間,建立指定時間。
@Test
public void from() {
OffsetTime offsetTime = OffsetTime.parse("06:01:04.123456789-05:00");
OffsetTime result = OffsetTime.from(offsetTime);
System.out.println(result);
assertEquals("06:01:04.123456789-05:00", result.toString());
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-11-11T06:01:04-05:00[Brazil/Acre]");
result = OffsetTime.from(zonedDateTime);
System.out.println(result);
assertEquals("06:01:04-05:00", result.toString());
}
06:01:04.123456789-05:00
06:01:04-05:00
InitializeOffsetTimeTest.java
OffsetTime Initialization Java 新增單元測試,驗證 Java OffsetTime Initialize 是否符合預期。
package org.ruoxue.java_147.datetime.offsettime;
import static org.junit.Assert.*;
import java.time.Clock;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.junit.Test;
public class InitializeOffsetTimeTest {
@Test
public void now() {
OffsetTime offsetTime = OffsetTime.now();
System.out.println(offsetTime);
ZoneId zone = ZoneId.of("Brazil/Acre");
offsetTime = OffsetTime.now(zone);
System.out.println(offsetTime);
zone = ZoneId.of("UTC-5");
Clock clock = Clock.system(zone);
offsetTime = OffsetTime.now(clock);
System.out.println(offsetTime);
zone = ZoneId.of("Etc/GMT+5");
offsetTime = OffsetTime.now(zone);
System.out.println(offsetTime);
}
@Test
public void of() {
ZoneOffset offset = ZoneOffset.ofHours(-5);
OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
System.out.println(offsetTime);
assertEquals("06:01:04-05:00", offsetTime.toString());
offsetTime = OffsetTime.of(LocalTime.of(6, 1, 4, 0), offset);
System.out.println(offsetTime);
assertEquals("06:01:04-05:00", offsetTime.toString());
}
@Test
public void parse() {
OffsetTime offsetTime = OffsetTime.parse("06:01:04-05:00");
System.out.println(offsetTime);
assertEquals("06:01:04-05:00", offsetTime.toString());
offsetTime = OffsetTime.parse("06:01:04.123456789-05:00");
System.out.println(offsetTime);
assertEquals("06:01:04.123456789-05:00", offsetTime.toString());
}
@Test
public void from() {
OffsetTime offsetTime = OffsetTime.parse("06:01:04.123456789-05:00");
OffsetTime result = OffsetTime.from(offsetTime);
System.out.println(result);
assertEquals("06:01:04.123456789-05:00", result.toString());
ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-11-11T06:01:04-05:00[Brazil/Acre]");
result = OffsetTime.from(zonedDateTime);
System.out.println(result);
assertEquals("06:01:04-05:00", result.toString());
}
}
心得分享
Java OffsetTime Initialize 儲存時間,精確到奈秒,以及帶有偏移量的時間,OffsetTime Initialization Java 提供了幾種 OffsetTime 初始化的操作範例,使用單元測試驗證產出結果。