Java ZonedDateTime Methods - Java 147

Java ZonedDateTime Methods – Java 147

Java ZonedDateTime Methods

具有時區概念的時間戳記,帶有時區的日期時間為不可變的物件, 實現 ChronoZonedDateTime 接口,介紹常見的 now 、 of 、 get 、 with 等方法, ZonedDateTime Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- datetime
       |                   +- zoneddatetime
       |                       +- ZonedDateTimeMethodsTest.java   

單元測試

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

now

ZonedDateTime Methods Java 建立一個 ZonedDateTime ,提供目前日期時間。

	@Test
	public void now() {
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println(zonedDateTime);

		ZoneId zone = ZoneId.of("Europe/Athens");
		zonedDateTime = ZonedDateTime.now(zone);
		System.out.println(zonedDateTime);

		zone = ZoneId.of("UTC+3");
		Clock clock = Clock.system(zone);
		zonedDateTime = ZonedDateTime.now(clock);
		System.out.println(zonedDateTime);

		zone = ZoneId.of("Etc/GMT-3");
		zonedDateTime = ZonedDateTime.now(zone);
		System.out.println(zonedDateTime);
	}
2023-07-04T20:20:07.730+08:00[Asia/Taipei]
2023-07-04T15:20:07.731+03:00[Europe/Athens]
2023-07-04T15:20:07.738+03:00[UTC+03:00]
2023-07-04T15:20:07.738+03:00[Etc/GMT-3]

of

ZonedDateTime Methods Java 建立一個 ZonedDateTime ,建立指定日期時間。

	@Test
	public void of() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());

		long epochSecond = zonedDateTime.toEpochSecond();
		System.out.println(epochSecond);
		assertEquals(1686024489L, epochSecond);

		zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());

		zonedDateTime = ZonedDateTime.of(LocalDate.of(2023, 6, 6), LocalTime.of(7, 8, 9, 0), zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
	}
2023-06-06T07:08:09+03:00[Europe/Athens]
1686024489
2023-06-06T07:08:09+03:00[Europe/Athens]
2023-06-06T07:08:09+03:00[Europe/Athens]

ofLocal

ZonedDateTime Methods Java 建立一個 ZonedDateTime ,使用 LocalDateTime 、ZoneId 建立指定日期時間。

	@Test
	public void ofLocal() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone,
				ZoneOffset.ofHours(3));
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
	}
2023-06-06T07:08:09+03:00[Europe/Athens]

ofStrict

ZonedDateTime Methods Java 建立一個 ZonedDateTime ,使用 LocalDateTime 、 ZoneOffset 建立指定日期時間。

	@Test
	public void ofStrict() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0),
				ZoneOffset.ofHours(3), zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
	}
2023-06-06T07:08:09+03:00[Europe/Athens]

get

ZonedDateTime Methods Java 建立一個 ZonedDateTime ,取得指定欄位,如:年、月、日等。

	@Test
	public void get() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		int year = zonedDateTime.get(ChronoField.YEAR);
		System.out.println(year);
		assertEquals(2023, year);

		int month = zonedDateTime.get(ChronoField.MONTH_OF_YEAR);
		System.out.println(month);
		assertEquals(6, month);

		int dayOfMonth = zonedDateTime.get(ChronoField.DAY_OF_MONTH);
		System.out.println(dayOfMonth);
		assertEquals(6, dayOfMonth);

		int dayOfWeek = zonedDateTime.get(ChronoField.DAY_OF_WEEK);
		System.out.println(dayOfWeek);
		assertEquals(2, dayOfWeek);

		int dayOfYear = zonedDateTime.get(ChronoField.DAY_OF_YEAR);
		System.out.println(dayOfYear);
		assertEquals(157, dayOfYear);

		int hourOfDay = zonedDateTime.get(ChronoField.HOUR_OF_DAY);
		System.out.println(hourOfDay);
		assertEquals(7, hourOfDay);

		int minuteOfHour = zonedDateTime.get(ChronoField.MINUTE_OF_HOUR);
		System.out.println(minuteOfHour);
		assertEquals(8, minuteOfHour);

		int secondOfMinute = zonedDateTime.get(ChronoField.SECOND_OF_MINUTE);
		System.out.println(secondOfMinute);
		assertEquals(9, secondOfMinute);
	}
2023
6
6
2
157
7
8
9

getValue

ZonedDateTime Methods in Java 建立一個 ZonedDateTime ,取得指定欄位,如:年、月、日等。

	@Test
	public void getValue() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		int year = zonedDateTime.getYear();
		System.out.println(year);
		assertEquals(2023, year);

		int month = zonedDateTime.getMonthValue();
		System.out.println(month);
		assertEquals(6, month);

		int dayOfMonth = zonedDateTime.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(6, dayOfMonth);

		int dayOfWeek = zonedDateTime.getDayOfWeek().getValue();
		System.out.println(dayOfWeek);
		assertEquals(2, dayOfWeek);

		int dayOfYear = zonedDateTime.getDayOfYear();
		System.out.println(dayOfYear);
		assertEquals(157, dayOfYear);

		int hour = zonedDateTime.getHour();
		System.out.println(hour);
		assertEquals(7, hour);

		int minute = zonedDateTime.getMinute();
		System.out.println(minute);
		assertEquals(8, minute);

		int second = zonedDateTime.getSecond();
		System.out.println(second);
		assertEquals(9, second);
	}
2023
6
6
2
157
7
8
9

with

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

	@Test
	public void with() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		ZonedDateTime result = zonedDateTime.withYear(2024);
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2024, year);

		result = zonedDateTime.withMonth(7);
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(7, month);

		result = zonedDateTime.withDayOfMonth(7);
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(7, dayOfMonth);

		result = zonedDateTime.withHour(2);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(2, hour);

		result = zonedDateTime.withMinute(3);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(3, minute);

		result = zonedDateTime.withSecond(4);
		int second = result.getSecond();
		System.out.println(second);
		assertEquals(4, second);
	}
2024
7
7
2
3
4

withTemporalAdjuster

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

	@Test
	public void withTemporalAdjuster() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		ZonedDateTime result = zonedDateTime.with(ChronoField.DAY_OF_MONTH, 7);
		System.out.println(result);
		assertEquals("2023-06-07T07:08:09+03:00[Europe/Athens]", result.toString());

		result = zonedDateTime.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
		System.out.println(result);
		assertEquals("2023-06-10T07:08:09+03:00[Europe/Athens]", result.toString());

		result = zonedDateTime.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println(result);
		assertEquals("2023-06-30T07:08:09+03:00[Europe/Athens]", result.toString());

		TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(14));
		result = zonedDateTime.with(temporalAdjuster);
		System.out.println(result);
		assertEquals("2023-06-20T07:08:09+03:00[Europe/Athens]", result.toString());

		result = zonedDateTime.with(t -> t.plus(Duration.ofHours(1)));
		System.out.println(result);
		assertEquals("2023-06-06T08:08:09+03:00[Europe/Athens]", result.toString());
	}
2023-06-07T07:08:09+03:00[Europe/Athens]
2023-06-10T07:08:09+03:00[Europe/Athens]
2023-06-30T07:08:09+03:00[Europe/Athens]
2023-06-20T07:08:09+03:00[Europe/Athens]
2023-06-06T08:08:09+03:00[Europe/Athens]

toLocalDate

ZonedDateTime Functions in Java 建立一個 ZonedDateTime ,取得日期。

	@Test
	public void toLocalDate() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		LocalDate result = zonedDateTime.toLocalDate();
		System.out.println(result);
		assertEquals("2023-06-06", result.toString());
	}
2023-06-06

toLocalTime

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

	@Test
	public void toLocalTime() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		LocalTime result = zonedDateTime.toLocalTime();
		System.out.println(result);
		assertEquals("07:08:09", result.toString());
	}
07:08:09

toLocalDateTime

ZonedDateTime Functions in Java 建立一個 ZonedDateTime ,取得日期時間, ZonedDateTime Java Methods 提供範例參考。

	@Test
	public void toLocalDateTime() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		LocalDateTime result = zonedDateTime.toLocalDateTime();
		System.out.println(result);
		assertEquals("2023-06-06T07:08:09", result.toString());
	}
2023-06-06T07:08:09

ZonedDateTimeMethodsTest.java

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

package org.ruoxue.java_147.datetime.zoneddatetime;

import static org.junit.Assert.*;

import java.time.Clock;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.LocalTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjuster;
import java.time.temporal.TemporalAdjusters;

import org.junit.Test;

public class ZonedDateTimeMethodsTest {

	@Test
	public void now() {
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println(zonedDateTime);

		ZoneId zone = ZoneId.of("Europe/Athens");
		zonedDateTime = ZonedDateTime.now(zone);
		System.out.println(zonedDateTime);

		zone = ZoneId.of("UTC+3");
		Clock clock = Clock.system(zone);
		zonedDateTime = ZonedDateTime.now(clock);
		System.out.println(zonedDateTime);

		zone = ZoneId.of("Etc/GMT-3");
		zonedDateTime = ZonedDateTime.now(zone);
		System.out.println(zonedDateTime);
	}

	@Test
	public void of() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());

		long epochSecond = zonedDateTime.toEpochSecond();
		System.out.println(epochSecond);
		assertEquals(1686024489L, epochSecond);

		zonedDateTime = ZonedDateTime.of(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());

		zonedDateTime = ZonedDateTime.of(LocalDate.of(2023, 6, 6), LocalTime.of(7, 8, 9, 0), zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
	}

	@Test
	public void ofLocal() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.ofLocal(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0), zone,
				ZoneOffset.ofHours(3));
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
	}

	@Test
	public void ofStrict() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.ofStrict(LocalDateTime.of(2023, 6, 6, 7, 8, 9, 0),
				ZoneOffset.ofHours(3), zone);
		System.out.println(zonedDateTime);
		assertEquals("2023-06-06T07:08:09+03:00[Europe/Athens]", zonedDateTime.toString());
	}

	@Test
	public void get() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		int year = zonedDateTime.get(ChronoField.YEAR);
		System.out.println(year);
		assertEquals(2023, year);

		int month = zonedDateTime.get(ChronoField.MONTH_OF_YEAR);
		System.out.println(month);
		assertEquals(6, month);

		int dayOfMonth = zonedDateTime.get(ChronoField.DAY_OF_MONTH);
		System.out.println(dayOfMonth);
		assertEquals(6, dayOfMonth);

		int dayOfWeek = zonedDateTime.get(ChronoField.DAY_OF_WEEK);
		System.out.println(dayOfWeek);
		assertEquals(2, dayOfWeek);

		int dayOfYear = zonedDateTime.get(ChronoField.DAY_OF_YEAR);
		System.out.println(dayOfYear);
		assertEquals(157, dayOfYear);

		int hourOfDay = zonedDateTime.get(ChronoField.HOUR_OF_DAY);
		System.out.println(hourOfDay);
		assertEquals(7, hourOfDay);

		int minuteOfHour = zonedDateTime.get(ChronoField.MINUTE_OF_HOUR);
		System.out.println(minuteOfHour);
		assertEquals(8, minuteOfHour);

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

	@Test
	public void getValue() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		int year = zonedDateTime.getYear();
		System.out.println(year);
		assertEquals(2023, year);

		int month = zonedDateTime.getMonthValue();
		System.out.println(month);
		assertEquals(6, month);

		int dayOfMonth = zonedDateTime.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(6, dayOfMonth);

		int dayOfWeek = zonedDateTime.getDayOfWeek().getValue();
		System.out.println(dayOfWeek);
		assertEquals(2, dayOfWeek);

		int dayOfYear = zonedDateTime.getDayOfYear();
		System.out.println(dayOfYear);
		assertEquals(157, dayOfYear);

		int hour = zonedDateTime.getHour();
		System.out.println(hour);
		assertEquals(7, hour);

		int minute = zonedDateTime.getMinute();
		System.out.println(minute);
		assertEquals(8, minute);

		int second = zonedDateTime.getSecond();
		System.out.println(second);
		assertEquals(9, second);
	}

	@Test
	public void with() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		ZonedDateTime result = zonedDateTime.withYear(2024);
		int year = result.getYear();
		System.out.println(year);
		assertEquals(2024, year);

		result = zonedDateTime.withMonth(7);
		int month = result.getMonthValue();
		System.out.println(month);
		assertEquals(7, month);

		result = zonedDateTime.withDayOfMonth(7);
		int dayOfMonth = result.getDayOfMonth();
		System.out.println(dayOfMonth);
		assertEquals(7, dayOfMonth);

		result = zonedDateTime.withHour(2);
		int hour = result.getHour();
		System.out.println(hour);
		assertEquals(2, hour);

		result = zonedDateTime.withMinute(3);
		int minute = result.getMinute();
		System.out.println(minute);
		assertEquals(3, minute);

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

	@Test
	public void withTemporalAdjuster() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		ZonedDateTime result = zonedDateTime.with(ChronoField.DAY_OF_MONTH, 7);
		System.out.println(result);
		assertEquals("2023-06-07T07:08:09+03:00[Europe/Athens]", result.toString());

		result = zonedDateTime.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
		System.out.println(result);
		assertEquals("2023-06-10T07:08:09+03:00[Europe/Athens]", result.toString());

		result = zonedDateTime.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println(result);
		assertEquals("2023-06-30T07:08:09+03:00[Europe/Athens]", result.toString());

		TemporalAdjuster temporalAdjuster = t -> t.plus(Period.ofDays(14));
		result = zonedDateTime.with(temporalAdjuster);
		System.out.println(result);
		assertEquals("2023-06-20T07:08:09+03:00[Europe/Athens]", result.toString());

		result = zonedDateTime.with(t -> t.plus(Duration.ofHours(1)));
		System.out.println(result);
		assertEquals("2023-06-06T08:08:09+03:00[Europe/Athens]", result.toString());
	}

	@Test
	public void toLocalDate() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		LocalDate result = zonedDateTime.toLocalDate();
		System.out.println(result);
		assertEquals("2023-06-06", result.toString());
	}

	@Test
	public void toLocalTime() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		LocalTime result = zonedDateTime.toLocalTime();
		System.out.println(result);
		assertEquals("07:08:09", result.toString());
	}

	@Test
	public void toLocalDateTime() {
		ZoneId zone = ZoneId.of("Europe/Athens");
		ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, 6, 6, 7, 8, 9, 0, zone);
		LocalDateTime result = zonedDateTime.toLocalDateTime();
		System.out.println(result);
		assertEquals("2023-06-06T07:08:09", result.toString());
	}
}

心得分享

ZonedDateTime Functions in Java 用於儲存日期和時間,精確到奈秒,以及帶有區域偏移量的時區,用於處理不明確的本地日期時間,熟悉 ZonedDateTime Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。

發佈留言