最初,我进行了相当简单的测试:
@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);
}它总是在默认的TimeZone UTC的PC上执行,并具有以下输出:
H2 db的记录:2016年8月08 :00:00:2016年世界协调时清华10月20日:2016年世界协调时20 :00清华2016年世界协调时间10月20日20 :00:2016年世界协调时2016年10月20日清华2016年世界协调时间10月20日-世界协调时间2016年10月20日:00:00
其中:dateRangeServiceImpl.findAll()实现为:
public List<DateRangeBean> findAll (){
List<DateRangeBean> result = new ArrayList<>();
dateRangeRepository.findAll().iterator().forEachRemaining(result::add);
return result;
}( dateRangeServiceImpl.findGreatedDateTo(Date date)
public List<DateRangeBean> test (Date currentDate){
List<DateRangeBean> result = new ArrayList<>();
dateRangeRepository.findGreatedDateTo(currentDate).iterator().forEachRemaining(result::add);
return result;
}dateRangeRepository -是作为Spring数据一部分的纯接口
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,测试失败,输出如下:
H2 db中的记录:2016年8月08 : 00:00:00 -2016年清华10月20 :00 MST 2016清华10月20 :00:00 MST 2016当前日期是: Mon Aug 08 17:00 MST 2016 Mon 08 00:00:00 MST 2016清华清华2016年10月20 :00:00 MST 2016 java.lang.AssertionError : 2预期:2实际:3实际:3
好吧,也许,这是合理的:我们定义了currentDate变量8月9日UTC,但是在下一步,我们在MST中将currentDate重新转换为currentDate_date,结果是8月8日。
但是:
Q1:
基于我们的@查询:drb.DateTo >= :currentDate,在当前情况下:monAug080000:00MST 2016 >= >=08月08 17:00MST 2016 --这个说法是不正确的!
假设Spring数据只比较日、月、年和trims时间段。是对的吗?
Q2:
我试图修复测试,将时区直接放到测试中:
public void testMe() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
..............
}但我还是得到了失败的结果
H2 db中的记录:2016年8月08 :00:2016年世界协调时间10月20日07:00清华2016年世界协调时间10月20日07:00 2016年世界协调时间10月20日07:00 2016年世界协调时间10月20日清华2016年世界协调时间10月20日07:00:2016年世界协调时清华10月20日07:00:2016年世界协调时java.lang.AssertionError:预计2但发现3:3
现在我不明白怎么可能:
星期一08 :07:00 2016年世界协调时 >= Tue 8月09 :00:00 UTC 2016?
Q3: i尝试将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"));
}考试通过了!
H2 db的记录:2016年8月08 :00:00:2016年世界协调时清华10月20日:2016年世界协调时20 :00清华2016年世界协调时间10月20日20 :00:2016年世界协调时2016年10月20日清华2016年世界协调时间10月20日-世界协调时间2016年10月20日:00:00
那么,有人能解释为什么在Spring上下文xml文件中指定默认的TZ并直接在测试方法中指定没有修复问题吗?
只有通过静态块指定TZ才解决了这个问题?
发布于 2016-07-31 08:30:03
我认为这可能是你的数据库问题,而不是你的应用程序
例如,在更改控制面板中的客户端位置后,Oracle数据库不接受来自客户端的连接。
https://stackoverflow.com/questions/38682111
复制相似问题