From f48b51a3b1634aab1833080a8eb18b5a5c9fd50e Mon Sep 17 00:00:00 2001
From: kankichi00 <135567744+kankichi00@users.noreply.github.com>
Date: Thu, 25 Jan 2024 10:29:02 +0900
Subject: [PATCH 1/2] =?UTF-8?q?SystemTimeUtil=E3=82=92LocalDateTime?=
=?UTF-8?q?=E5=AF=BE=E5=BF=9C=E3=81=95=E3=81=9B=E3=81=9F=20(#52)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat: LocalDateTime,LocalDateを取得できるようにする
* test: LocalDateTime,LocalDateの取得のテストを追加
* feat: レビュー指摘の修正
* test: レビュー指摘の修正とInspection対応
* style:改行コードをv6developに合わせて修正
---
.../nablarch/core/date/SystemTimeUtil.java | 29 +++++
.../core/date/SystemTimeUtilTest.java | 102 +++++++++++++-----
2 files changed, 102 insertions(+), 29 deletions(-)
diff --git a/src/main/java/nablarch/core/date/SystemTimeUtil.java b/src/main/java/nablarch/core/date/SystemTimeUtil.java
index e4bbcf0..23c9dff 100644
--- a/src/main/java/nablarch/core/date/SystemTimeUtil.java
+++ b/src/main/java/nablarch/core/date/SystemTimeUtil.java
@@ -2,8 +2,11 @@
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
import java.util.Date;
+import java.util.TimeZone;
+import nablarch.core.ThreadContext;
import nablarch.core.repository.SystemRepository;
import nablarch.core.util.annotation.Published;
@@ -53,6 +56,17 @@ public static Timestamp getTimestamp() {
return getProvider().getTimestamp();
}
+ /**
+ * システム日時を取得する。
+ *
+ * @return システム日時
+ */
+ public static LocalDateTime getLocalDateTime(){
+ TimeZone tz = getTimeZone();
+
+ return getTimestamp().toInstant().atZone(tz.toZoneId()).toLocalDateTime();
+ }
+
/**
* システム日付を yyyyMMdd 形式の文字列で取得する。
*
@@ -99,4 +113,19 @@ private static SystemTimeProvider getProvider() {
}
return provider;
}
+
+ /**
+ * タイムゾーンを取得する。
+ *
+ *
設定されているタイムゾーンを取得する。
+ *
+ * @return スレッドコンテキストに設定されているタイムゾーン。スレッドコンテキストにタイムゾーンが設定されていない場合は、システムデフォルトのタイムゾーン。
+ */
+ private static TimeZone getTimeZone(){
+ TimeZone timeZone = ThreadContext.getTimeZone();
+ if (timeZone != null) {
+ return timeZone;
+ }
+ return TimeZone.getDefault();
+ }
}
diff --git a/src/test/java/nablarch/core/date/SystemTimeUtilTest.java b/src/test/java/nablarch/core/date/SystemTimeUtilTest.java
index 7a93c24..1dfce2c 100644
--- a/src/test/java/nablarch/core/date/SystemTimeUtilTest.java
+++ b/src/test/java/nablarch/core/date/SystemTimeUtilTest.java
@@ -1,31 +1,34 @@
package nablarch.core.date;
import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThrows;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
-import java.util.Map;
+import java.util.TimeZone;
-import nablarch.core.repository.ObjectLoader;
+import nablarch.core.ThreadContext;
import nablarch.core.repository.SystemRepository;
import nablarch.util.FixedSystemTimeProvider;
-import org.junit.Assert;
+import org.junit.After;
+import org.hamcrest.MatcherAssert;
import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
/**
* {@link SystemTimeUtil}のテストクラス
* @author Miki Habu
*/
public class SystemTimeUtilTest {
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
+
+ private TimeZone defaultTimeZone;
/**
* テスト実施前準備
@@ -33,19 +36,26 @@ public class SystemTimeUtilTest {
*/
@Before
public void setUp() {
+ defaultTimeZone = TimeZone.getDefault();
// リポジトリの初期化
- SystemRepository.load(new ObjectLoader() {
- @Override
- public Map load() {
- HashMap result = new HashMap();
- FixedSystemTimeProvider provider = new FixedSystemTimeProvider();
- provider.setFixedDate("20110107123456");
- result.put("systemTimeProvider", provider);
- return result;
- }
+ SystemRepository.load(() -> {
+ HashMap result = new HashMap<>();
+ FixedSystemTimeProvider provider = new FixedSystemTimeProvider();
+ provider.setFixedDate("20110107123456");
+ result.put("systemTimeProvider", provider);
+ return result;
});
}
-
+
+ /**
+ * テスト実施後処理
+ *
+ */
+ @After
+ public void tearDown() {
+ TimeZone.setDefault(defaultTimeZone);
+ }
+
/**
* {@link SystemTimeUtil#getDate()}のテスト
*
@@ -55,22 +65,19 @@ public Map load() {
public void testGetDate() throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
Date expected = sdf.parse("20110107123456000");
- Assert.assertThat(SystemTimeUtil.getDate(), is(expected));
+ MatcherAssert.assertThat(SystemTimeUtil.getDate(), is(expected));
}
/**
* {@link SystemTimeUtil#getDate()} のテスト。
*
* リポジトリに値がない場合、例外を送出するかどうか。
- * @throws Exception
*/
@Test
- public void testGetDateErr() throws Exception {
+ public void testGetDateErr() {
SystemRepository.clear();
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage("specified systemTimeProvider is not registered in SystemRepository.");
-
- SystemTimeUtil.getDate();
+ IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, SystemTimeUtil::getDate);
+ MatcherAssert.assertThat(exception.getMessage(), is("specified systemTimeProvider is not registered in SystemRepository."));
}
/**
@@ -79,7 +86,44 @@ public void testGetDateErr() throws Exception {
@Test
public void testGetTimestamp() {
Timestamp expected = Timestamp.valueOf("2011-01-07 12:34:56.000000000");
- Assert.assertThat(SystemTimeUtil.getTimestamp(), is(expected));
+ MatcherAssert.assertThat(SystemTimeUtil.getTimestamp(), is(expected));
+ }
+
+ /**
+ * {@link SystemTimeUtil#getLocalDateTime()}のテスト
+ *
+ * スレッドコンテキストにタイムゾーンが設定されていない場合、システムデフォルトのタイムゾーンを使用して{@link LocalDateTime}を取得できること。
+ */
+ @Test
+ public void testGetLocalDateTimeDefault() {
+ ThreadContext.setTimeZone(null);
+ TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
+
+ LocalDateTime expected = ZonedDateTime
+ .parse(
+ "2011-01-07T12:34:56",
+ DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(defaultTimeZone.toZoneId())
+ )// 文字列を、システムデフォルトのタイムゾーンの日時と考えるようにしたうえで変換
+ .withZoneSameInstant(ZoneId.of("America/Los_Angeles"))// タイムゾーンをアメリカに変更
+ .toLocalDateTime();
+
+ MatcherAssert.assertThat(SystemTimeUtil.getLocalDateTime(), is(expected));
+ }
+
+ /**
+ * {@link SystemTimeUtil#getLocalDateTime()} のテスト。
+ *
+ * スレッドコンテキストにタイムゾーンが設定されている場合、スレッドコンテキストのタイムゾーンを使用する。
+ */
+ @Test
+ public void testGetLocalDateTime() {
+ ThreadContext.setTimeZone(defaultTimeZone);
+ TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
+
+ DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
+ LocalDateTime expected = LocalDateTime.parse("20110107123456", dtf);
+
+ MatcherAssert.assertThat(SystemTimeUtil.getLocalDateTime(), is(expected));
}
/**
@@ -87,7 +131,7 @@ public void testGetTimestamp() {
*/
@Test
public void testGetCurrentDateString() {
- Assert.assertThat(SystemTimeUtil.getDateString(), is("20110107"));
+ MatcherAssert.assertThat(SystemTimeUtil.getDateString(), is("20110107"));
}
/**
@@ -95,7 +139,7 @@ public void testGetCurrentDateString() {
*/
@Test
public void testGetDateTimeString() {
- Assert.assertThat(SystemTimeUtil.getDateTimeString(), is("20110107123456"));
+ MatcherAssert.assertThat(SystemTimeUtil.getDateTimeString(), is("20110107123456"));
}
/**
@@ -103,7 +147,7 @@ public void testGetDateTimeString() {
*/
@Test
public void testGetDateTimeMillisString() {
- Assert.assertThat(SystemTimeUtil.getDateTimeMillisString(), is("20110107123456000"));
+ MatcherAssert.assertThat(SystemTimeUtil.getDateTimeMillisString(), is("20110107123456000"));
}
}
From 4992bcc0331edaf3069fb11495ef4ef5b008a9b1 Mon Sep 17 00:00:00 2001
From: hijiri <157558988+tishijiri@users.noreply.github.com>
Date: Tue, 12 Mar 2024 13:21:54 +0900
Subject: [PATCH 2/2] =?UTF-8?q?6u1=E3=83=AA=E3=83=AA=E3=83=BC=E3=82=B9?=
=?UTF-8?q?=E7=94=A8=E3=81=AE=E3=83=90=E3=83=BC=E3=82=B8=E3=83=A7=E3=83=B3?=
=?UTF-8?q?=E3=81=AB=E5=A4=89=E6=9B=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index ee201c0..7af814a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.nablarch.framework
nablarch-core
- 6-NEXT-SNAPSHOT
+ 2.1.0
scm:git:git://github.com/nablarch/${project.artifactId}.git
@@ -17,7 +17,7 @@
com.nablarch
nablarch-parent
- 6-NEXT-SNAPSHOT
+ 6u1