Java OffsetDateTime Class - Java 147

Java OffsetDateTime Class – Java 147

Java OffsetDateTime Class

儲存日期和時間,精確到奈秒,以及帶有偏移量的日期時間,提供增加、減少、比較日期時間、或兩個日期時間差值的方法, OffsetDateTime Class 介紹常見的 plus 、 minus 、 isBefore 、 isAfter 、 until 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- datetime
       |                   +- offsetdatetime
       |                       +- OffsetDateTimeClassTest.java   

單元測試

OffsetDateTime Class Java 提供日期時間加減、比較前後等操作 OffsetDateTime 。

plus

OffsetDateTime Class Java 建立一個 OffsetDateTime ,提供指定日期時間加法運算。

	@Test
	public void plus() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.plusYears(1);
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2024, year);

		result = offsetDateTime.plusMonths(1);
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(1, month);

		result = offsetDateTime.plusDays(1);
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(26, dayOfMonth);

		result = offsetDateTime.plusHours(1);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(6, hour);

		result = offsetDateTime.plusMinutes(1);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(5, minute);

		result = offsetDateTime.plusSeconds(1);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}
2024
1
26
6
5
4

plusTemporalAmount

OffsetDateTime Class Java 建立一個 OffsetDateTime ,使用 Period 、 Duration 提供指定日期時間加法運算。

	@Test
	public void plusTemporalAmount() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.plus(Period.ofYears(1));
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2024, year);

		result = offsetDateTime.plus(Period.ofMonths(1));
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(1, month);

		result = offsetDateTime.plus(Period.ofDays(1));
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(26, dayOfMonth);

		result = offsetDateTime.plus(Duration.ofHours(1));
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(6, hour);

		result = offsetDateTime.plus(Duration.ofMinutes(1));
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(5, minute);

		result = offsetDateTime.plus(Duration.ofSeconds(1));
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}
2024
1
26
6
5

minus

OffsetDateTime Class Java 建立一個 OffsetDateTime ,提供指定日期時間減法運算。

	@Test
	public void minus() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.minusYears(1);
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2022, year);

		result = offsetDateTime.minusMonths(1);
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(11, month);

		result = offsetDateTime.minusDays(1);
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(24, dayOfMonth);

		result = offsetDateTime.minusHours(1);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(4, hour);

		result = offsetDateTime.minusMinutes(1);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(3, minute);

		result = offsetDateTime.minusSeconds(1);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(2, second);
	}
2022
11
24
4
3
2

minusTemporalAmount

OffsetDateTime Class Java 建立一個 OffsetDateTime ,使用 Period 、 Duration 提供指定日期時間減法運算。

	@Test
	public void minusTemporalAmount() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.minus(Period.ofYears(1));
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2022, year);

		result = offsetDateTime.minus(Period.ofMonths(1));
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(11, month);

		result = offsetDateTime.minus(Period.ofDays(1));
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(24, dayOfMonth);

		result = offsetDateTime.minus(Duration.ofHours(1));
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(4, hour);

		result = offsetDateTime.minus(Duration.ofMinutes(1));
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(3, minute);

		result = offsetDateTime.minus(Duration.ofSeconds(1));
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(2, second);
	}
2022
11
24
4
3
2

isBefore

OffsetDateTime Class in Java 建立兩個 OffsetDateTime ,日期時間比對前後。

	@Test
	public void isBefore() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2024, 12, 25, 5, 4, 3, 0, offset);
		boolean result = offsetDateTime.isBefore(offsetDateTime2);
		System.out.println(result);
		assertTrue(result);

		result = offsetDateTime2.isBefore(offsetDateTime);
		System.out.println(result);
		assertFalse(result);
	}
true
false

isAfter

OffsetDateTime Class in Java 建立兩個 OffsetDateTime ,日期時間比對前後, Java OffsetDateTime Class Example 提供範例參考。

	@Test
	public void isAfter() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2022, 12, 25, 5, 4, 3, 0, offset);
		boolean result = offsetDateTime.isAfter(offsetDateTime2);
		System.out.println(result);
		assertTrue(result);

		result = offsetDateTime2.isAfter(offsetDateTime);
		System.out.println(result);
		assertFalse(result);
	}
true
false

until

OffsetDateTime Class in Java 建立兩個 OffsetDateTime ,取得日期時間的差值, Java OffsetDateTime Class Example 提供範例參考。

	@Test
	public void until() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2023, 12, 26, 5, 4, 3, 0, offset);
		long result = offsetDateTime.until(offsetDateTime2, ChronoUnit.DAYS);
		System.out.println(result);
		assertEquals(1, result);

		result = offsetDateTime.until(offsetDateTime2, ChronoUnit.HOURS);
		System.out.println(result);
		assertEquals(24, result);

		result = offsetDateTime.until(offsetDateTime2, ChronoUnit.MINUTES);
		System.out.println(result);
		assertEquals(1440, result);
	}
1
24
1440

OffsetDateTimeClassTest.java

OffsetDateTime Class in Java 新增單元測試,驗證 Java OffsetDateTime Class Example 是否符合預期。

package org.ruoxue.java_147.datetime.offsetdatetime;

import static org.junit.Assert.*;

import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;

import org.junit.Test;

public class OffsetDateTimeClassTest {

	@Test
	public void plus() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.plusYears(1);
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2024, year);

		result = offsetDateTime.plusMonths(1);
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(1, month);

		result = offsetDateTime.plusDays(1);
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(26, dayOfMonth);

		result = offsetDateTime.plusHours(1);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(6, hour);

		result = offsetDateTime.plusMinutes(1);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(5, minute);

		result = offsetDateTime.plusSeconds(1);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}

	@Test
	public void plusTemporalAmount() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.plus(Period.ofYears(1));
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2024, year);

		result = offsetDateTime.plus(Period.ofMonths(1));
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(1, month);

		result = offsetDateTime.plus(Period.ofDays(1));
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(26, dayOfMonth);

		result = offsetDateTime.plus(Duration.ofHours(1));
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(6, hour);

		result = offsetDateTime.plus(Duration.ofMinutes(1));
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(5, minute);

		result = offsetDateTime.plus(Duration.ofSeconds(1));
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}

	@Test
	public void minus() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.minusYears(1);
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2022, year);

		result = offsetDateTime.minusMonths(1);
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(11, month);

		result = offsetDateTime.minusDays(1);
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(24, dayOfMonth);

		result = offsetDateTime.minusHours(1);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(4, hour);

		result = offsetDateTime.minusMinutes(1);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(3, minute);

		result = offsetDateTime.minusSeconds(1);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(2, second);
	}

	@Test
	public void minusTemporalAmount() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime result = offsetDateTime.minus(Period.ofYears(1));
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2022, year);

		result = offsetDateTime.minus(Period.ofMonths(1));
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(11, month);

		result = offsetDateTime.minus(Period.ofDays(1));
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(24, dayOfMonth);

		result = offsetDateTime.minus(Duration.ofHours(1));
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(4, hour);

		result = offsetDateTime.minus(Duration.ofMinutes(1));
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(3, minute);

		result = offsetDateTime.minus(Duration.ofSeconds(1));
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(2, second);
	}

	@Test
	public void isBefore() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2024, 12, 25, 5, 4, 3, 0, offset);
		boolean result = offsetDateTime.isBefore(offsetDateTime2);
		System.out.println(result);
		assertTrue(result);

		result = offsetDateTime2.isBefore(offsetDateTime);
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void isAfter() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2022, 12, 25, 5, 4, 3, 0, offset);
		boolean result = offsetDateTime.isAfter(offsetDateTime2);
		System.out.println(result);
		assertTrue(result);

		result = offsetDateTime2.isAfter(offsetDateTime);
		System.out.println(result);
		assertFalse(result);
	}

	@Test
	public void until() {
		ZoneOffset offset = ZoneOffset.ofHours(2);
		OffsetDateTime offsetDateTime = OffsetDateTime.of(2023, 12, 25, 5, 4, 3, 0, offset);
		OffsetDateTime offsetDateTime2 = OffsetDateTime.of(2023, 12, 26, 5, 4, 3, 0, offset);
		long result = offsetDateTime.until(offsetDateTime2, ChronoUnit.DAYS);
		System.out.println(result);
		assertEquals(1, result);

		result = offsetDateTime.until(offsetDateTime2, ChronoUnit.HOURS);
		System.out.println(result);
		assertEquals(24, result);

		result = offsetDateTime.until(offsetDateTime2, ChronoUnit.MINUTES);
		System.out.println(result);
		assertEquals(1440, result);
	}
}

心得分享

Java OffsetDateTime Class Example 帶有偏移量的日期時間,物件的值不會被改變,可以同時在不同的執行緒下被使用,儲存日期和時間,精確到奈秒以及與 UTC/格林威治的偏移量, OffsetDateTime Class in Java 提供 plus 、 minus 、 isBefore 、 isAfter 等操作範例。

發佈留言