Java OffsetTime Methods - Java 147

Java OffsetTime Methods – Java 147

Java OffsetTime Methods

表示在 ISO-8601 日曆系統中與 UTC / Greenwich 的偏移量時間,例如:06:01:04-05:00,物件的值不會被改變,可以同時在不同的執行緒下被使用,介紹常見的 now 、 of 、 get 、 with 等方法, OffsetTime Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- datetime
       |                   +- offsettime
       |                       +- OffsetTimeMethodsTest.java   

單元測試

OffsetTime Methods Java 提供目前時間、建立、取得等操作 OffsetTime 。

now

OffsetTime Methods Java 建立一個 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);
	}
17:31:23.716+08:00
04:31:23.716-05:00
04:31:23.716-05:00
04:31:23.716-05:00

of

OffsetTime Methods Java 建立一個 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

ofInstant

OffsetTime Methods Java 建立一個 OffsetTime ,使用 Instant 建立指定時間。

	@Test
	public void ofInstant() {
		ZoneId zone = ZoneId.of("Brazil/Acre");
		OffsetTime offsetTime = OffsetTime.ofInstant(Instant.parse("2023-11-11T06:01:04Z"), zone);
		System.out.println(offsetTime);
		assertEquals("01:01:04-05:00", offsetTime.toString());
	}
01:01:04-05:00

get

OffsetTime Methods Java 建立一個 OffsetTime ,取得指定欄位,如:時、分、秒 。

	@Test
	public void get() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);

		int hourOfDay = offsetTime.get(ChronoField.HOUR_OF_DAY);
		System.out.println(hourOfDay);
		assertEquals(6, hourOfDay);

		int minuteOfHour = offsetTime.get(ChronoField.MINUTE_OF_HOUR);
		System.out.println(minuteOfHour);
		assertEquals(1, minuteOfHour);

		int secondOfMinute = offsetTime.get(ChronoField.SECOND_OF_MINUTE);
		System.out.println(secondOfMinute);
		assertEquals(4, secondOfMinute);
	}
6
1
4

getValue

OffsetTime Methods Java 建立一個 OffsetTime ,取得指定欄位,如:時、分、秒 。

	@Test
	public void getValue() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);

		int hour = offsetTime.getHour();
		System.out.println(hour);
		assertEquals(6, hour);

		int minute = offsetTime.getMinute();
		System.out.println(minute);
		assertEquals(1, minute);

		int second = offsetTime.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}
6
1
4

with

OffsetTime Methods in Java 建立一個 OffsetTime ,調整指定欄位,如:時、分、秒等,會傳回新物件。

	@Test
	public void with() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);

		OffsetTime result = offsetTime.withHour(7);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(7, hour);

		result = offsetTime.withMinute(2);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(2, minute);

		result = offsetTime.withSecond(5);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(5, second);
	}
7
2
5

withTemporalAdjuster

OffsetTime Methods in Java 建立一個 OffsetTime ,使用 TemporalAdjuster 調整指定欄位,如:年、月、日等,會傳回新物件。

	@Test
	public void withTemporalAdjuster() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
		
		OffsetTime result = offsetTime.with(ChronoField.HOUR_OF_DAY, 7);
		System.out.println(result);
		assertEquals("07:01:04-05:00", result.toString());

		TemporalAdjuster temporalAdjuster = t -> t.plus(Duration.ofHours(2));
		result = offsetTime.with(temporalAdjuster);
		System.out.println(result);
		assertEquals("08:01:04-05:00", result.toString());

		result = offsetTime.with(t -> t.plus(Duration.ofMinutes(1)));
		System.out.println(result);
		assertEquals("06:02:04-05:00", result.toString());

		result = offsetTime.with(t -> t.plus(Duration.ofSeconds(1)));
		System.out.println(result);
		assertEquals("06:01:05-05:00", result.toString());
	}
07:01:04-05:00
08:01:04-05:00
06:02:04-05:00
06:01:05-05:00

toLocalTime

OffsetTime Functions in Java 建立一個 OffsetTime ,取得時間。

	@Test
	public void toLocalTime() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
		LocalTime result = offsetTime.toLocalTime();
		System.out.println(result);
		assertEquals("06:01:04", result.toString());
	}
06:01:04

OffsetTimeMethodsTest.java

OffsetTime Methods in Java 新增單元測試,驗證 OffsetTime Functions in Java 是否符合預期。

package org.ruoxue.java_147.datetime.offsettime;

import static org.junit.Assert.*;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjuster;

import org.junit.Test;

public class OffsetTimeMethodsTest {

	@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 ofInstant() {
		ZoneId zone = ZoneId.of("Brazil/Acre");
		OffsetTime offsetTime = OffsetTime.ofInstant(Instant.parse("2023-11-11T06:01:04Z"), zone);
		System.out.println(offsetTime);
		assertEquals("01:01:04-05:00", offsetTime.toString());
	}

	@Test
	public void get() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);

		int hourOfDay = offsetTime.get(ChronoField.HOUR_OF_DAY);
		System.out.println(hourOfDay);
		assertEquals(6, hourOfDay);

		int minuteOfHour = offsetTime.get(ChronoField.MINUTE_OF_HOUR);
		System.out.println(minuteOfHour);
		assertEquals(1, minuteOfHour);

		int secondOfMinute = offsetTime.get(ChronoField.SECOND_OF_MINUTE);
		System.out.println(secondOfMinute);
		assertEquals(4, secondOfMinute);
	}

	@Test
	public void getValue() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);

		int hour = offsetTime.getHour();
		System.out.println(hour);
		assertEquals(6, hour);

		int minute = offsetTime.getMinute();
		System.out.println(minute);
		assertEquals(1, minute);

		int second = offsetTime.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}

	@Test
	public void with() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);

		OffsetTime result = offsetTime.withHour(7);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(7, hour);

		result = offsetTime.withMinute(2);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(2, minute);

		result = offsetTime.withSecond(5);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(5, second);
	}

	@Test
	public void withTemporalAdjuster() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
		
		OffsetTime result = offsetTime.with(ChronoField.HOUR_OF_DAY, 7);
		System.out.println(result);
		assertEquals("07:01:04-05:00", result.toString());

		TemporalAdjuster temporalAdjuster = t -> t.plus(Duration.ofHours(2));
		result = offsetTime.with(temporalAdjuster);
		System.out.println(result);
		assertEquals("08:01:04-05:00", result.toString());

		result = offsetTime.with(t -> t.plus(Duration.ofMinutes(1)));
		System.out.println(result);
		assertEquals("06:02:04-05:00", result.toString());

		result = offsetTime.with(t -> t.plus(Duration.ofSeconds(1)));
		System.out.println(result);
		assertEquals("06:01:05-05:00", result.toString());
	}

	@Test
	public void toLocalTime() {
		ZoneOffset offset = ZoneOffset.ofHours(-5);
		OffsetTime offsetTime = OffsetTime.of(6, 1, 4, 0, offset);
		LocalTime result = offsetTime.toLocalTime();
		System.out.println(result);
		assertEquals("06:01:04", result.toString());
	}
}

心得分享

OffsetTime Functions in Java 是一個不可變的時間物件,表示時間,通常被視為小時,分鐘,秒偏移。繼承 Object 類別並實現 Comparable 接口,熟悉 OffsetTime Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。

發佈留言