File tree Expand file tree Collapse file tree
main/java/org/mapstruct/ap/internal/conversion
test/java/org/mapstruct/ap/test/conversion/java8time Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -230,6 +230,7 @@ private void registerJava8TimeConversions() {
230230 // Java 8 to Date
231231 register ( JavaTimeConstants .ZONED_DATE_TIME_FQN , Date .class , new JavaZonedDateTimeToDateConversion () );
232232 register ( JavaTimeConstants .LOCAL_DATE_TIME_FQN , Date .class , new JavaLocalDateTimeToDateConversion () );
233+ register ( JavaTimeConstants .LOCAL_DATE_FQN , Date .class , new JavaLocalDateToDateConversion () );
233234
234235 }
235236
Original file line number Diff line number Diff line change 1+ /**
2+ * Copyright 2012-2016 Gunnar Morling (http://www.gunnarmorling.de/)
3+ * and/or other contributors as indicated by the @authors tag. See the
4+ * copyright.txt file in the distribution for a full listing of all
5+ * contributors.
6+ *
7+ * Licensed under the Apache License, Version 2.0 (the "License");
8+ * you may not use this file except in compliance with the License.
9+ * You may obtain a copy of the License at
10+ *
11+ * http://www.apache.org/licenses/LICENSE-2.0
12+ *
13+ * Unless required by applicable law or agreed to in writing, software
14+ * distributed under the License is distributed on an "AS IS" BASIS,
15+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ * See the License for the specific language governing permissions and
17+ * limitations under the License.
18+ */
19+ package org .mapstruct .ap .internal .conversion ;
20+
21+ import org .mapstruct .ap .internal .model .common .ConversionContext ;
22+
23+ /**
24+ * SimpleConversion for mapping {@link java.time.LocalDate} to
25+ * {@link java.util.Date} and vice versa.
26+ */
27+ public class JavaLocalDateToDateConversion extends SimpleConversion {
28+
29+ @ Override
30+ protected String getToExpression (ConversionContext conversionContext ) {
31+ return "java.util.Date.from( <SOURCE>.atStartOfDay( java.time.ZoneOffset.UTC ).toInstant() )" ;
32+
33+ }
34+
35+ @ Override
36+ protected String getFromExpression (ConversionContext conversionContext ) {
37+ return "java.time.LocalDateTime.ofInstant( <SOURCE>.toInstant(), java.time.ZoneOffset.UTC ).toLocalDate()" ;
38+ }
39+ }
Original file line number Diff line number Diff line change @@ -267,4 +267,28 @@ public void testLocalDateTimeToDateMapping() {
267267
268268 assertThat ( source .getForDateConversionWithLocalDateTime () ).isEqualTo ( dateTime );
269269 }
270+
271+ @ Test
272+ public void testLocalDateToDateMapping () {
273+ TimeZone .setDefault ( TimeZone .getTimeZone ( "Australia/Melbourne" ) );
274+
275+ Source source = new Source ();
276+ LocalDate localDate = LocalDate .of ( 2016 , 3 , 1 );
277+ source .setForDateConversionWithLocalDate ( localDate );
278+
279+ Target target = SourceTargetMapper .INSTANCE .sourceToTargetDefaultMapping ( source );
280+
281+ assertThat ( target .getForDateConversionWithLocalDate () ).isNotNull ();
282+
283+ Calendar instance = Calendar .getInstance ( TimeZone .getTimeZone ( "UTC" ) );
284+ instance .setTimeInMillis ( target .getForDateConversionWithLocalDate ().getTime () );
285+
286+ assertThat ( instance .get ( Calendar .YEAR ) ).isEqualTo ( localDate .getYear () );
287+ assertThat ( instance .get ( Calendar .MONTH ) ).isEqualTo ( localDate .getMonthValue () - 1 );
288+ assertThat ( instance .get ( Calendar .DATE ) ).isEqualTo ( localDate .getDayOfMonth () );
289+
290+ source = SourceTargetMapper .INSTANCE .targetToSource ( target );
291+
292+ assertThat ( source .getForDateConversionWithLocalDate () ).isEqualTo ( localDate );
293+ }
270294}
Original file line number Diff line number Diff line change @@ -42,6 +42,8 @@ public class Source {
4242
4343 private LocalDateTime forDateConversionWithLocalDateTime ;
4444
45+ private LocalDate forDateConversionWithLocalDate ;
46+
4547 public ZonedDateTime getZonedDateTime () {
4648 return zonedDateTime ;
4749 }
@@ -97,4 +99,12 @@ public LocalDateTime getForDateConversionWithLocalDateTime() {
9799 public void setForDateConversionWithLocalDateTime (LocalDateTime forDateConversionWithLocalDateTime ) {
98100 this .forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime ;
99101 }
102+
103+ public LocalDate getForDateConversionWithLocalDate () {
104+ return forDateConversionWithLocalDate ;
105+ }
106+
107+ public void setForDateConversionWithLocalDate (LocalDate forDateConversionWithLocalDate ) {
108+ this .forDateConversionWithLocalDate = forDateConversionWithLocalDate ;
109+ }
100110}
Original file line number Diff line number Diff line change @@ -39,6 +39,8 @@ public class Target {
3939
4040 private Date forDateConversionWithLocalDateTime ;
4141
42+ private Date forDateConversionWithLocalDate ;
43+
4244 public String getZonedDateTime () {
4345 return zonedDateTime ;
4446 }
@@ -95,4 +97,11 @@ public void setForDateConversionWithLocalDateTime(Date forDateConversionWithLoca
9597 this .forDateConversionWithLocalDateTime = forDateConversionWithLocalDateTime ;
9698 }
9799
100+ public Date getForDateConversionWithLocalDate () {
101+ return forDateConversionWithLocalDate ;
102+ }
103+
104+ public void setForDateConversionWithLocalDate (Date forDateConversionWithLocalDate ) {
105+ this .forDateConversionWithLocalDate = forDateConversionWithLocalDate ;
106+ }
98107}
You can’t perform that action at this time.
0 commit comments