#1595 Support for conversion between java.time.Instant and java.util.Date (#1622)

This commit is contained in:
neoXfire 2018-10-12 21:10:25 +02:00 committed by Sjaak Derksen
parent 4f539d2a08
commit 6b3cbaae9e
6 changed files with 78 additions and 0 deletions

View File

@ -222,6 +222,7 @@ public class Conversions {
register( JavaTimeConstants.ZONED_DATE_TIME_FQN, Date.class, new JavaZonedDateTimeToDateConversion() );
register( JavaTimeConstants.LOCAL_DATE_TIME_FQN, Date.class, new JavaLocalDateTimeToDateConversion() );
register( JavaTimeConstants.LOCAL_DATE_FQN, Date.class, new JavaLocalDateToDateConversion() );
register( JavaTimeConstants.INSTANT, Date.class, new JavaInstantToDateConversion() );
}

View File

@ -0,0 +1,37 @@
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.internal.conversion;
import org.mapstruct.ap.internal.model.common.ConversionContext;
import org.mapstruct.ap.internal.model.common.Type;
import org.mapstruct.ap.internal.util.Collections;
import java.util.Date;
import java.util.Set;
import static org.mapstruct.ap.internal.conversion.ConversionUtils.date;
/**
* SimpleConversion for mapping {@link java.time.Instant} to
* {@link Date} and vice versa.
*/
public class JavaInstantToDateConversion extends SimpleConversion {
@Override
protected String getToExpression(ConversionContext conversionContext) {
return date( conversionContext ) + ".from( <SOURCE> )";
}
@Override
protected Set<Type> getToConversionImportTypes(ConversionContext conversionContext) {
return Collections.asSet( conversionContext.getTypeFactory().getType( Date.class ) );
}
@Override
protected String getFromExpression(ConversionContext conversionContext) {
return "<SOURCE>.toInstant()";
}
}

View File

@ -20,6 +20,8 @@ public final class JavaTimeConstants {
public static final String DATE_TIME_FORMATTER_FQN = "java.time.format.DateTimeFormatter";
public static final String INSTANT = "java.time.Instant";
private JavaTimeConstants() {
}
}

View File

@ -7,12 +7,14 @@ package org.mapstruct.ap.test.conversion.java8time;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import org.junit.Test;
@ -229,6 +231,21 @@ public class Java8TimeConversionTest {
assertThat( source.getForDateConversionWithZonedDateTime() ).isEqualTo( dateTime );
}
@Test
public void testInstantToDateMapping() {
Instant instant = Instant.ofEpochMilli( 1539366615000L );
Source source = new Source();
source.setForDateConversionWithInstant( instant );
Target target = SourceTargetMapper.INSTANCE.sourceToTargetDefaultMapping( source );
Date date = target.getForDateConversionWithInstant();
assertThat( date ).isNotNull();
assertThat( date.getTime() ).isEqualTo( 1539366615000L );
source = SourceTargetMapper.INSTANCE.targetToSource( target );
assertThat( source.getForDateConversionWithInstant() ).isEqualTo( instant );
}
@Test
public void testLocalDateTimeToDateMapping() {
TimeZone.setDefault( TimeZone.getTimeZone( "Australia/Melbourne" ) );

View File

@ -5,6 +5,7 @@
*/
package org.mapstruct.ap.test.conversion.java8time;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@ -31,6 +32,8 @@ public class Source {
private LocalDate forDateConversionWithLocalDate;
private Instant forDateConversionWithInstant;
public ZonedDateTime getZonedDateTime() {
return zonedDateTime;
}
@ -94,4 +97,12 @@ public class Source {
public void setForDateConversionWithLocalDate(LocalDate forDateConversionWithLocalDate) {
this.forDateConversionWithLocalDate = forDateConversionWithLocalDate;
}
public Instant getForDateConversionWithInstant() {
return forDateConversionWithInstant;
}
public void setForDateConversionWithInstant(Instant forDateConversionWithInstant) {
this.forDateConversionWithInstant = forDateConversionWithInstant;
}
}

View File

@ -29,6 +29,8 @@ public class Target {
private Date forDateConversionWithLocalDate;
private Date forDateConversionWithInstant;
public String getZonedDateTime() {
return zonedDateTime;
}
@ -92,4 +94,12 @@ public class Target {
public void setForDateConversionWithLocalDate(Date forDateConversionWithLocalDate) {
this.forDateConversionWithLocalDate = forDateConversionWithLocalDate;
}
public Date getForDateConversionWithInstant() {
return forDateConversionWithInstant;
}
public void setForDateConversionWithInstant(Date forDateConversionWithInstant) {
this.forDateConversionWithInstant = forDateConversionWithInstant;
}
}