起初,我有很簡單的測試:Java的,失敗的測試PC上改變時區後
@Test
public void testMe() {
System.out.println("Records in H2 db:");
List<DateRangeBean> all = dateRangeServiceImpl.findAll();
all.forEach(x -> System.out.println(x.getDateTo()));
Clock.system(ZoneId.of("UTC"));
ZonedDateTime currentDate = ZonedDateTime.of(2016, 8, 9, 0, 0, 0, 0, clock.getZone());
Date currentDate_date = Date.from(currentDate.toInstant());
System.out.println("Current Date is: " + currentDate_date);
List<DateRangeBean> result = new ArrayList<>();
dateRangeServiceImpl.findGreaterDateTo(currentDate_date).iterator().forEachRemaining(result::add);
result.forEach(x -> System.out.println(x.getDateTo()));
assertEquals(result.size(), 2);
}
始終是在PC上使用的默認時區UTC與下面的輸出執行:
Records in H2 db: Mon Aug 08 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016 Current Date is: Tue Aug 09 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016
其中: dateRangeServiceImpl.findAll()實現爲:
public List<DateRangeBean> findAll(){
List<DateRangeBean> result = new ArrayList<>();
dateRangeRepository.findAll().iterator().forEachRemaining(result::add);
return result;
}
dateRangeServiceImpl.findGreatedDateTo(日期日期):
public List<DateRangeBean> test (Date currentDate){
List<DateRangeBean> result = new ArrayList<>();
dateRangeRepository.findGreatedDateTo(currentDate).iterator().forEachRemaining(result::add);
return result;
}
和dateRangeRepository - 是純接口作爲彈簧數據
public interface DateRangeRepository extends PagingAndSortingRepository<DateRangeBean, Long> {
@Query("from DateRangeBean drb where (drb.dateTo >= :currentDate)")
List<DateRangeBean> findGreatedDateTo(@Param("currentDate") Date currentDate);
}
和DateRangeBean的一部分:
@Entity
public class DateRangeBean implements java.io.Serializable {
@Temporal(TemporalType.DATE)
@Column(name = "date_To", length = 10)
private Date dateTo;
.....................
public Date getDateTo() {
return new Date(this.dateTo.getTime());
}
public void setDateTo(Date dateTo) {
this.dateTo = new Date(dateTo.getTime());
}
} 今天我試圖用時區運行在PC這個測試:UTC -07:00和測試失敗,出現以下的輸出:
Records in H2 db: Mon Aug 08 00:00:00 MST 2016 Thu Oct 20 00:00:00 MST 2016 Thu Oct 20 00:00:00 MST 2016 Current Date is: Mon Aug 08 17:00:00 MST 2016 Mon Aug 08 00:00:00 MST 2016 Thu Oct 20 00:00:00 MST 2016 Thu Oct 20 00:00:00 MST 2016 java.lang.AssertionError: expected [2] but found [3] Expected :2 Actual :3
奧基,也許,這是合理的:我們定義了currentDate變量8月9日UTC TZ,但在下一步我們將currentDate重新轉換爲currentDate_date在MST TZ中,結果爲8月8日。
但是:
Q1:
基於我們@Query:drb.DateTo> =:的currentdate在當前情況下 :週一8月08日00:00:00 MST 2016> =週一8月08 17:00 :00 MST 2016 - 此聲明不正確!
假設,即春季數據進行比較只有日,月,年和修剪時間段。 是否正確?
Q2:
我試圖修復測試並直接將時區測試:
public void testMe() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
..............
}
但我仍然得到失敗的結果:
Records in H2 db: Mon Aug 08 07:00:00 UTC 2016 Thu Oct 20 07:00:00 UTC 2016 Thu Oct 20 07:00:00 UTC 2016 Current Date is: Tue Aug 09 00:00:00 UTC 2016 Mon Aug 08 07:00:00 UTC 2016 Thu Oct 20 07:00:00 UTC 2016 Thu Oct 20 07:00:00 UTC 2016 java.lang.AssertionError: expected [2] but found [3]
而目前我無法理解h ow是可能的:
Mon Aug 08 07:00:00 UTC 2016> = Tue Aug 09 00:00:00 UTC 2016?
Q3: 我試圖指定TZ爲Spring上下文的一部分:
<bean id="defaultZoneInfo" class="sun.util.calendar.ZoneInfo" factory-method="getTimeZone">
<constructor-arg type="java.lang.String" value="UTC"/>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="java.util.TimeZone.setDefault"/>
<property name="arguments">
<list>
<ref bean="defaultZoneInfo"/>
</list>
</property>
</bean>
,但結果是一樣的,與Q2。
Q4: 靜態塊指定TZ:
static {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}
和測試 - 通過!
Records in H2 db: Mon Aug 08 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016 Current Date is: Tue Aug 09 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016 Thu Oct 20 00:00:00 UTC 2016
因此,有人可以解釋爲什麼在Spring上下文XML文件,並直接在測試方法中指定默認TZ沒有解決問題呢?
只有通過靜態塊指定TZ解決了這個問題?
也許你是對的,但我需要找到適當的解決方案來解決這個問題。順便說一句,測試我使用嵌入式h2分貝。 – user471011