Initialize DateTimeFormatter in Java - Java 147

Initialize DateTimeFormatter in Java – Java 147

Initialize DateTimeFormatter in Java

初始化 DateTimeFormatter 物件的值不會被改變,可以同時在不同的執行緒下被使用,介紹常見的 ofPattern 、 ofLocalizedDate 等方法,了解 Initialize DateTimeFormatter Java 的不同操作和方法,本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- datetime
       |                   +- datetimeformatter
       |                       +- InitializeDateTimeFormatterTest.java   

單元測試

Java Initialize DateTimeFormatter 提供初始化操作 DateTimeFormatter 。

Pattern symbols

自定義模式是使用在格式化或解析時,具有特殊意義的字母。

SymbolDescriptionExample
y年份2023
M月份1-12
MMM月份縮寫Jan
MMMM完整的月份名稱January
d一月中的某一天1-31
E星期幾縮寫Mon
EEEE完整的星期名稱Monday
h小時0-11
H小時0-23
m分鐘0-59
s0-59
S秒的小數部分0-9
a AM / PM,早上或下午AM
z時區縮寫PST
Z時區偏移+0800

ofPattern

Java Initialize DateTimeFormatter 建立一個 DateTimeFormatter ,提供樣版,格式化日期時間。

	@Test
	public void ofPattern() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
		String result = formatter.format(LocalDateTime.now());
		System.out.println(result);

		formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		result = formatter.format(LocalDate.now());
		System.out.println(result);

		formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
		result = formatter.format(LocalTime.now());
		System.out.println(result);
	}
2023-06-25T21:40:32
2023-06-25
21:40:32

ofLocalizedDate

Java Initialize DateTimeFormatter 建立一個 DateTimeFormatter ,提供本地化樣版,使用 FormatStyle 格式化日期時間。

	@Test
	public void ofLocalizedDate() {
		LocalDateTime localDateTime = LocalDateTime.parse("2023-03-14T09:08:07.123456789");
		DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
		String result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("2023年3月14日 星期二", result);

		formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
		result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("上午09時08分07秒", result);

		formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
		result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("2023年3月14日 上午09時08分07秒", result);

		formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
		result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("2023/3/14 上午 9:08", result);
	}
2023年3月14日 星期二
上午09時08分07秒
2023年3月14日 上午09時08分07秒
2023/3/14 上午 9:08

predefined

Java Initialize DateTimeFormatter 使用預先定義的樣版,格式化時間日期。

	@Test
	public void predefined() {
		LocalDateTime localDateTime = LocalDateTime.parse("2023-03-14T09:08:07.123456789");
		OffsetDateTime offsetDateTime = OffsetDateTime.parse("2023-03-14T09:08:07.123456789+08:00");
		ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]");

		String result = DateTimeFormatter.BASIC_ISO_DATE.format(localDateTime);
		System.out.println("BASIC_ISO_DATE: " + result);
		assertEquals("20230314", result);

		result = DateTimeFormatter.ISO_LOCAL_DATE.format(localDateTime);
		System.out.println("ISO_LOCAL_DATE: " + result);
		assertEquals("2023-03-14", result);

		result = DateTimeFormatter.ISO_OFFSET_DATE.format(offsetDateTime);
		System.out.println("ISO_OFFSET_DATE: " + result);
		assertEquals("2023-03-14+08:00", result);

		result = DateTimeFormatter.ISO_DATE.format(localDateTime);
		System.out.println("ISO_DATE: " + result);
		assertEquals("2023-03-14", result);

		result = DateTimeFormatter.ISO_LOCAL_TIME.format(localDateTime);
		System.out.println("ISO_LOCAL_TIME: " + result);
		assertEquals("09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_OFFSET_TIME.format(offsetDateTime);
		System.out.println("ISO_OFFSET_TIME: " + result);
		assertEquals("09:08:07.123456789+08:00", result);

		result = DateTimeFormatter.ISO_TIME.format(localDateTime);
		System.out.println("ISO_TIME: " + result);
		assertEquals("09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime);
		System.out.println("ISO_LOCAL_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
		System.out.println("ISO_OFFSET_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789+08:00", result);

		result = DateTimeFormatter.ISO_DATE_TIME.format(localDateTime);
		System.out.println("ISO_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_DATE_TIME.format(zonedDateTime);
		System.out.println("ISO_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]", result);

		result = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime);
		System.out.println("ISO_ZONED_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]", result);

		result = DateTimeFormatter.ISO_ORDINAL_DATE.format(zonedDateTime);
		System.out.println("ISO_ORDINAL_DATE: " + result);
		assertEquals("2023-073+08:00", result);

		result = DateTimeFormatter.ISO_WEEK_DATE.format(zonedDateTime);
		System.out.println("ISO_WEEK_DATE: " + result);
		assertEquals("2023-W11-2+08:00", result);

		result = DateTimeFormatter.ISO_INSTANT.format(zonedDateTime);
		System.out.println("ISO_INSTANT: " + result);
		assertEquals("2023-03-14T01:08:07.123456789Z", result);

		result = DateTimeFormatter.RFC_1123_DATE_TIME.format(zonedDateTime);
		System.out.println("RFC_1123_DATE_TIME: " + result);
		assertEquals("Tue, 14 Mar 2023 09:08:07 +0800", result);
	}
BASIC_ISO_DATE: 20230314
ISO_LOCAL_DATE: 2023-03-14
ISO_OFFSET_DATE: 2023-03-14+08:00
ISO_DATE: 2023-03-14
ISO_LOCAL_TIME: 09:08:07.123456789
ISO_OFFSET_TIME: 09:08:07.123456789+08:00
ISO_TIME: 09:08:07.123456789
ISO_LOCAL_DATE_TIME: 2023-03-14T09:08:07.123456789
ISO_OFFSET_DATE_TIME: 2023-03-14T09:08:07.123456789+08:00
ISO_DATE_TIME: 2023-03-14T09:08:07.123456789
ISO_DATE_TIME: 2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]
ISO_ZONED_DATE_TIME: 2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]
ISO_ORDINAL_DATE: 2023-073+08:00
ISO_WEEK_DATE: 2023-W11-2+08:00
ISO_INSTANT: 2023-03-14T01:08:07.123456789Z
RFC_1123_DATE_TIME: Tue, 14 Mar 2023 09:08:07 +0800

InitializeDateTimeFormatterTest.java

DateTimeFormatter Initialization Java 新增單元測試,驗證 Java DateTimeFormatter Initialize 是否符合預期。

package org.ruoxue.java_147.datetime.datetimeformatter;

import static org.junit.Assert.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

import org.junit.Test;

public class InitializeDateTimeFormatterTest {

	@Test
	public void ofPattern() {
		DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
		String result = formatter.format(LocalDateTime.now());
		System.out.println(result);

		formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		result = formatter.format(LocalDate.now());
		System.out.println(result);

		formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
		result = formatter.format(LocalTime.now());
		System.out.println(result);
	}

	@Test
	public void ofLocalizedDate() {
		LocalDateTime localDateTime = LocalDateTime.parse("2023-03-14T09:08:07.123456789");
		DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
		String result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("2023年3月14日 星期二", result);

		formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
		result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("上午09時08分07秒", result);

		formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
		result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("2023年3月14日 上午09時08分07秒", result);

		formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.SHORT);
		result = formatter.format(localDateTime);
		System.out.println(result);
		assertEquals("2023/3/14 上午 9:08", result);
	}

	@Test
	public void predefined() {
		LocalDateTime localDateTime = LocalDateTime.parse("2023-03-14T09:08:07.123456789");
		OffsetDateTime offsetDateTime = OffsetDateTime.parse("2023-03-14T09:08:07.123456789+08:00");
		ZonedDateTime zonedDateTime = ZonedDateTime.parse("2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]");

		String result = DateTimeFormatter.BASIC_ISO_DATE.format(localDateTime);
		System.out.println("BASIC_ISO_DATE: " + result);
		assertEquals("20230314", result);

		result = DateTimeFormatter.ISO_LOCAL_DATE.format(localDateTime);
		System.out.println("ISO_LOCAL_DATE: " + result);
		assertEquals("2023-03-14", result);

		result = DateTimeFormatter.ISO_OFFSET_DATE.format(offsetDateTime);
		System.out.println("ISO_OFFSET_DATE: " + result);
		assertEquals("2023-03-14+08:00", result);

		result = DateTimeFormatter.ISO_DATE.format(localDateTime);
		System.out.println("ISO_DATE: " + result);
		assertEquals("2023-03-14", result);

		result = DateTimeFormatter.ISO_LOCAL_TIME.format(localDateTime);
		System.out.println("ISO_LOCAL_TIME: " + result);
		assertEquals("09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_OFFSET_TIME.format(offsetDateTime);
		System.out.println("ISO_OFFSET_TIME: " + result);
		assertEquals("09:08:07.123456789+08:00", result);

		result = DateTimeFormatter.ISO_TIME.format(localDateTime);
		System.out.println("ISO_TIME: " + result);
		assertEquals("09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime);
		System.out.println("ISO_LOCAL_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(offsetDateTime);
		System.out.println("ISO_OFFSET_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789+08:00", result);

		result = DateTimeFormatter.ISO_DATE_TIME.format(localDateTime);
		System.out.println("ISO_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789", result);

		result = DateTimeFormatter.ISO_DATE_TIME.format(zonedDateTime);
		System.out.println("ISO_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]", result);

		result = DateTimeFormatter.ISO_ZONED_DATE_TIME.format(zonedDateTime);
		System.out.println("ISO_ZONED_DATE_TIME: " + result);
		assertEquals("2023-03-14T09:08:07.123456789+08:00[Asia/Singapore]", result);

		result = DateTimeFormatter.ISO_ORDINAL_DATE.format(zonedDateTime);
		System.out.println("ISO_ORDINAL_DATE: " + result);
		assertEquals("2023-073+08:00", result);

		result = DateTimeFormatter.ISO_WEEK_DATE.format(zonedDateTime);
		System.out.println("ISO_WEEK_DATE: " + result);
		assertEquals("2023-W11-2+08:00", result);

		result = DateTimeFormatter.ISO_INSTANT.format(zonedDateTime);
		System.out.println("ISO_INSTANT: " + result);
		assertEquals("2023-03-14T01:08:07.123456789Z", result);

		result = DateTimeFormatter.RFC_1123_DATE_TIME.format(zonedDateTime);
		System.out.println("RFC_1123_DATE_TIME: " + result);
		assertEquals("Tue, 14 Mar 2023 09:08:07 +0800", result);
	}
}

心得分享

Java DateTimeFormatter Initialize 新的日期時間格式化 API,讓日期和時間進行解析或格式化時更加便利,DateTimeFormatter Initialization Java 提供了幾種 DateTimeFormatter 初始化的操作範例,使用單元測試驗證產出結果。

發佈留言