mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#852 Expanding converters to cover java 8 LocalDate to java.util.Date
This commit is contained in:
parent
c36f4577ab
commit
e13696172e
@ -230,6 +230,7 @@ public class Conversions {
|
|||||||
// Java 8 to Date
|
// Java 8 to Date
|
||||||
register( JavaTimeConstants.ZONED_DATE_TIME_FQN, Date.class, new JavaZonedDateTimeToDateConversion() );
|
register( JavaTimeConstants.ZONED_DATE_TIME_FQN, Date.class, new JavaZonedDateTimeToDateConversion() );
|
||||||
register( JavaTimeConstants.LOCAL_DATE_TIME_FQN, Date.class, new JavaLocalDateTimeToDateConversion() );
|
register( JavaTimeConstants.LOCAL_DATE_TIME_FQN, Date.class, new JavaLocalDateTimeToDateConversion() );
|
||||||
|
register( JavaTimeConstants.LOCAL_DATE_FQN, Date.class, new JavaLocalDateToDateConversion() );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,39 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||||
|
* and/or other contributors as indicated by the @authors tag. See the
|
||||||
|
* copyright.txt file in the distribution for a full listing of all
|
||||||
|
* contributors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package org.mapstruct.ap.internal.conversion;
|
||||||
|
|
||||||
|
import org.mapstruct.ap.internal.model.common.ConversionContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SimpleConversion for mapping {@link java.time.LocalDate} to
|
||||||
|
* {@link java.util.Date} and vice versa.
|
||||||
|
*/
|
||||||
|
public class JavaLocalDateToDateConversion extends SimpleConversion {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getToExpression(ConversionContext conversionContext) {
|
||||||
|
return "java.util.Date.from( <SOURCE>.atStartOfDay( java.time.ZoneOffset.UTC ).toInstant() )";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getFromExpression(ConversionContext conversionContext) {
|
||||||
|
return "java.time.LocalDateTime.ofInstant( <SOURCE>.toInstant(), java.time.ZoneOffset.UTC ).toLocalDate()";
|
||||||
|
}
|
||||||
|
}
|
@ -267,4 +267,28 @@ public class Java8TimeConversionTest {
|
|||||||
|
|
||||||
assertThat( source.getForDateConversionWithLocalDateTime() ).isEqualTo( dateTime );
|
assertThat( source.getForDateConversionWithLocalDateTime() ).isEqualTo( dateTime );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLocalDateToDateMapping() {
|
||||||
|
TimeZone.setDefault( TimeZone.getTimeZone( "Australia/Melbourne" ) );
|
||||||
|
|
||||||
|
Source source = new Source();
|
||||||
|
LocalDate localDate = LocalDate.of( 2016, 3, 1 );
|
||||||
|
source.setForDateConversionWithLocalDate( localDate );
|
||||||
|
|
||||||
|
Target target = SourceTargetMapper.INSTANCE.sourceToTargetDefaultMapping( source );
|
||||||
|
|
||||||
|
assertThat( target.getForDateConversionWithLocalDate() ).isNotNull();
|
||||||
|
|
||||||
|
Calendar instance = Calendar.getInstance( TimeZone.getTimeZone( "UTC" ) );
|
||||||
|
instance.setTimeInMillis( target.getForDateConversionWithLocalDate().getTime() );
|
||||||
|
|
||||||
|
assertThat( instance.get( Calendar.YEAR ) ).isEqualTo( localDate.getYear() );
|
||||||
|
assertThat( instance.get( Calendar.MONTH ) ).isEqualTo( localDate.getMonthValue() - 1 );
|
||||||
|
assertThat( instance.get( Calendar.DATE ) ).isEqualTo( localDate.getDayOfMonth() );
|
||||||
|
|
||||||
|
source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||||
|
|
||||||
|
assertThat( source.getForDateConversionWithLocalDate() ).isEqualTo( localDate );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,8 @@ public class Source {
|
|||||||
|
|
||||||
private LocalDateTime forDateConversionWithLocalDateTime;
|
private LocalDateTime forDateConversionWithLocalDateTime;
|
||||||
|
|
||||||
|
private LocalDate forDateConversionWithLocalDate;
|
||||||
|
|
||||||
public ZonedDateTime getZonedDateTime() {
|
public ZonedDateTime getZonedDateTime() {
|
||||||
return zonedDateTime;
|
return zonedDateTime;
|
||||||
}
|
}
|
||||||
@ -97,4 +99,12 @@ public class Source {
|
|||||||
public void setForDateConversionWithLocalDateTime(LocalDateTime forDateConversionWithLocalDateTime) {
|
public void setForDateConversionWithLocalDateTime(LocalDateTime forDateConversionWithLocalDateTime) {
|
||||||
this.forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime;
|
this.forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LocalDate getForDateConversionWithLocalDate() {
|
||||||
|
return forDateConversionWithLocalDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setForDateConversionWithLocalDate(LocalDate forDateConversionWithLocalDate) {
|
||||||
|
this.forDateConversionWithLocalDate = forDateConversionWithLocalDate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,8 @@ public class Target {
|
|||||||
|
|
||||||
private Date forDateConversionWithLocalDateTime;
|
private Date forDateConversionWithLocalDateTime;
|
||||||
|
|
||||||
|
private Date forDateConversionWithLocalDate;
|
||||||
|
|
||||||
public String getZonedDateTime() {
|
public String getZonedDateTime() {
|
||||||
return zonedDateTime;
|
return zonedDateTime;
|
||||||
}
|
}
|
||||||
@ -95,4 +97,11 @@ public class Target {
|
|||||||
this.forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime;
|
this.forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Date getForDateConversionWithLocalDate() {
|
||||||
|
return forDateConversionWithLocalDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setForDateConversionWithLocalDate(Date forDateConversionWithLocalDate) {
|
||||||
|
this.forDateConversionWithLocalDate = forDateConversionWithLocalDate;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user