org.hibernate.type.descriptor.java.ZonedDateTimeJavaDescriptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
Hibernate's core ORM functionality
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.type.descriptor.java;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.hibernate.type.ZonedDateTimeType;
import org.hibernate.type.descriptor.WrapperOptions;
/**
* Java type descriptor for the LocalDateTime type.
*
* @author Steve Ebersole
*/
public class ZonedDateTimeJavaDescriptor extends AbstractTypeDescriptor {
/**
* Singleton access
*/
public static final ZonedDateTimeJavaDescriptor INSTANCE = new ZonedDateTimeJavaDescriptor();
@SuppressWarnings("unchecked")
public ZonedDateTimeJavaDescriptor() {
super( ZonedDateTime.class, ImmutableMutabilityPlan.INSTANCE );
}
@Override
public String toString(ZonedDateTime value) {
return ZonedDateTimeType.FORMATTER.format( value );
}
@Override
public ZonedDateTime fromString(String string) {
return ZonedDateTime.from( ZonedDateTimeType.FORMATTER.parse( string ) );
}
@Override
@SuppressWarnings("unchecked")
public X unwrap(ZonedDateTime zonedDateTime, Class type, WrapperOptions options) {
if ( zonedDateTime == null ) {
return null;
}
if ( ZonedDateTime.class.isAssignableFrom( type ) ) {
return (X) zonedDateTime;
}
if ( Calendar.class.isAssignableFrom( type ) ) {
return (X) GregorianCalendar.from( zonedDateTime );
}
if ( Timestamp.class.isAssignableFrom( type ) ) {
/*
* Workaround for HHH-13266 (JDK-8061577).
* Ideally we'd want to use Timestamp.from( zonedDateTime.toInstant() ), but this won't always work.
* Timestamp.from() assumes the number of milliseconds since the epoch
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
*/
return (X) Timestamp.valueOf( zonedDateTime.withZoneSameInstant( ZoneId.systemDefault() ).toLocalDateTime() );
}
if ( java.sql.Date.class.isAssignableFrom( type ) ) {
return (X) java.sql.Date.from( zonedDateTime.toInstant() );
}
if ( java.sql.Time.class.isAssignableFrom( type ) ) {
return (X) java.sql.Time.from( zonedDateTime.toInstant() );
}
if ( Date.class.isAssignableFrom( type ) ) {
return (X) Date.from( zonedDateTime.toInstant() );
}
if ( Long.class.isAssignableFrom( type ) ) {
return (X) Long.valueOf( zonedDateTime.toInstant().toEpochMilli() );
}
throw unknownUnwrap( type );
}
@Override
public ZonedDateTime wrap(X value, WrapperOptions options) {
if ( value == null ) {
return null;
}
if ( ZonedDateTime.class.isInstance( value ) ) {
return (ZonedDateTime) value;
}
if ( java.sql.Timestamp.class.isInstance( value ) ) {
final Timestamp ts = (Timestamp) value;
/*
* Workaround for HHH-13266 (JDK-8061577).
* Ideally we'd want to use ZonedDateTime.ofInstant( ts.toInstant(), ... ), but this won't always work.
* ts.toInstant() assumes the number of milliseconds since the epoch
* means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.
*/
return ts.toLocalDateTime().atZone( ZoneId.systemDefault() );
}
if ( java.util.Date.class.isInstance( value ) ) {
final java.util.Date date = (java.util.Date) value;
return ZonedDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );
}
if ( Long.class.isInstance( value ) ) {
return ZonedDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() );
}
if ( Calendar.class.isInstance( value ) ) {
final Calendar calendar = (Calendar) value;
return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );
}
throw unknownWrap( value.getClass() );
}
}