mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Add ScalarTypeLocalDate
This commit is contained in:
@@ -10,6 +10,7 @@ import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@@ -146,6 +147,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
this.extraTypeFactory = new DefaultTypeFactory(config);
|
||||
|
||||
initialiseStandard(clobType, blobType, config.isUuidStoreAsBinary());
|
||||
initialiseJavaTimeTypes();
|
||||
initialiseJodaTypes();
|
||||
|
||||
if (bootupClasses != null) {
|
||||
@@ -602,6 +604,15 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
return propParamTypes[1];
|
||||
}
|
||||
|
||||
protected void initialiseJavaTimeTypes() {
|
||||
if (ClassUtil.isPresent("java.time.LocalDate", this.getClass())) {
|
||||
logger.debug("Registering java.time data types");
|
||||
typeMap.put(java.time.LocalDate.class, new ScalarTypeLocalDate());
|
||||
typeMap.put(java.time.LocalDateTime.class, new ScalarTypeLocalDateTime());
|
||||
typeMap.put(OffsetDateTime.class, new ScalarTypeOffsetDateTime());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect if Joda classes are in the classpath and if so register the Joda
|
||||
* data types.
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.LocalDate. This maps to a JDBC Date.
|
||||
*/
|
||||
public class ScalarTypeLocalDate extends ScalarTypeBaseDate<LocalDate> {
|
||||
|
||||
public ScalarTypeLocalDate() {
|
||||
super(LocalDate.class, false, Types.DATE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return convertToDate((LocalDate) value).toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate convertFromDate(Date ts) {
|
||||
return ts.toLocalDate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToDate(LocalDate t) {
|
||||
return Date.valueOf(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof LocalDate) {
|
||||
return Date.valueOf((LocalDate) value);
|
||||
}
|
||||
return BasicTypeConverter.toDate(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate toBeanType(Object value) {
|
||||
if (value instanceof java.sql.Date) {
|
||||
return ((java.sql.Date) value).toLocalDate();
|
||||
}
|
||||
return (LocalDate) value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDate parseDateTime(long systemTimeMillis) {
|
||||
return new Timestamp(systemTimeMillis).toLocalDateTime().toLocalDate();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.*;
|
||||
|
||||
/**
|
||||
* ScalarType for java.sql.Timestamp.
|
||||
*/
|
||||
public class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTime> {
|
||||
|
||||
public ScalarTypeLocalDateTime() {
|
||||
super(LocalDateTime.class, true, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((LocalDateTime) value).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime convertFromTimestamp(Timestamp ts) {
|
||||
return ts.toLocalDateTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(LocalDateTime t) {
|
||||
|
||||
ZonedDateTime zonedDateTime = t.atZone(ZoneId.systemDefault());
|
||||
return Timestamp.from(zonedDateTime.toInstant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Timestamp) return value;
|
||||
return convertToTimestamp((LocalDateTime)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime toBeanType(Object value) {
|
||||
if (value instanceof LocalDateTime) return (LocalDateTime)value;
|
||||
return convertFromTimestamp((Timestamp)value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
/**
|
||||
* ScalarType for java.sql.Timestamp.
|
||||
*/
|
||||
public class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime<OffsetDateTime> {
|
||||
|
||||
public ScalarTypeOffsetDateTime() {
|
||||
super(OffsetDateTime.class, true, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((OffsetDateTime) value).toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime convertFromTimestamp(Timestamp ts) {
|
||||
return OffsetDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(OffsetDateTime t) {
|
||||
return Timestamp.from(t.toInstant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Timestamp) return value;
|
||||
return convertToTimestamp((OffsetDateTime)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime toBeanType(Object value) {
|
||||
if (value instanceof OffsetDateTime) return (OffsetDateTime) value;
|
||||
return convertFromTimestamp((Timestamp)value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user