DATACMNS-867 - Favor LocalDateTime over ZonedDateTime in CurrentDateTimeProvider.

Use LocalDateTime to avoid timezone issues just because the default auditing cannot handle it when the domain type property comes from eg. jodatime.
This commit is contained in:
Christoph Strobl
2017-02-06 15:27:03 +01:00
committed by Oliver Gierke
parent 378baba06a
commit d363e8f2c2
3 changed files with 26 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
*/
package org.springframework.data.auditing;
import java.time.ZonedDateTime;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAccessor;
import java.util.Optional;
@@ -25,6 +25,7 @@ import org.joda.time.DateTime;
* Default {@link DateTimeProvider} simply creating new {@link DateTime} instances for each method call.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.5
*/
public enum CurrentDateTimeProvider implements DateTimeProvider {
@@ -37,6 +38,6 @@ public enum CurrentDateTimeProvider implements DateTimeProvider {
*/
@Override
public Optional<TemporalAccessor> getNow() {
return Optional.of(ZonedDateTime.now());
return Optional.of(LocalDateTime.now());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -38,6 +38,7 @@ import org.springframework.util.Assert;
* A factory class to {@link AuditableBeanWrapper} instances.
*
* @author Oliver Gierke
* @author Christoph Strobl
* @since 1.5
*/
class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory {
@@ -190,6 +191,11 @@ class DefaultAuditableBeanWrapperFactory implements AuditableBeanWrapperFactory
if (conversionService.canConvert(Date.class, targetType)) {
if(!conversionService.canConvert(it.getClass(), Date.class)) {
throw new IllegalArgumentException(String.format("Cannot convert date type for member %s! From %s to java.util.Date to %s.",
source, it.getClass(), targetType));
}
Date date = conversionService.convert(it, Date.class);
return conversionService.convert(date, targetType);
}