NIFI-3664: - Updating the unmarshalling to consider the current date when parsing the time.

This closes #1647.

Signed-off-by: Bryan Bende <bbende@apache.org>
This commit is contained in:
Matt Gilman 2017-03-30 20:53:33 +01:00 committed by Bryan Bende
parent a565484ddd
commit f5f54c468c
No known key found for this signature in database
GPG Key ID: A0DDA9ED50711C39
2 changed files with 43 additions and 14 deletions

View File

@ -17,7 +17,12 @@
package org.apache.nifi.web.api.dto.util; package org.apache.nifi.web.api.dto.util;
import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
@ -29,18 +34,26 @@ public class TimeAdapter extends XmlAdapter<String, Date> {
public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss z"; public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss z";
private static final ZoneId ZONE_ID = TimeZone.getDefault().toZoneId();
@Override @Override
public String marshal(Date date) throws Exception { public String marshal(Date date) throws Exception {
final SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.US); final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT, Locale.US);
formatter.setTimeZone(TimeZone.getDefault()); final ZonedDateTime localDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZONE_ID);
return formatter.format(date); return formatter.format(localDateTime);
} }
@Override @Override
public Date unmarshal(String date) throws Exception { public Date unmarshal(String date) throws Exception {
final SimpleDateFormat parser = new SimpleDateFormat(DEFAULT_TIME_FORMAT, Locale.US); final LocalDateTime now = LocalDateTime.now();
parser.setTimeZone(TimeZone.getDefault()); final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_TIME_FORMAT)
return parser.parse(date); .parseDefaulting(ChronoField.YEAR, now.getYear())
.parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue())
.parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth())
.parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
.toFormatter(Locale.US);
final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser);
return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now)));
} }
} }

View File

@ -17,7 +17,12 @@
package org.apache.nifi.web.api.dto.util; package org.apache.nifi.web.api.dto.util;
import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.annotation.adapters.XmlAdapter;
import java.text.SimpleDateFormat; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date; import java.util.Date;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
@ -29,18 +34,29 @@ public class TimezoneAdapter extends XmlAdapter<String, Date> {
public static final String DEFAULT_DATE_TIME_FORMAT = "z"; public static final String DEFAULT_DATE_TIME_FORMAT = "z";
private static final ZoneId ZONE_ID = TimeZone.getDefault().toZoneId();
@Override @Override
public String marshal(Date date) throws Exception { public String marshal(Date date) throws Exception {
final SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT, Locale.US); final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT, Locale.US);
formatter.setTimeZone(TimeZone.getDefault()); final ZonedDateTime localDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZONE_ID);
return formatter.format(date); return formatter.format(localDateTime);
} }
@Override @Override
public Date unmarshal(String date) throws Exception { public Date unmarshal(String date) throws Exception {
final SimpleDateFormat parser = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT, Locale.US); final LocalDateTime now = LocalDateTime.now();
parser.setTimeZone(TimeZone.getDefault()); final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_DATE_TIME_FORMAT)
return parser.parse(date); .parseDefaulting(ChronoField.YEAR, now.getYear())
.parseDefaulting(ChronoField.MONTH_OF_YEAR, now.getMonthValue())
.parseDefaulting(ChronoField.DAY_OF_MONTH, now.getDayOfMonth())
.parseDefaulting(ChronoField.HOUR_OF_DAY, now.getHour())
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, now.getMinute())
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, now.getSecond())
.parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
.toFormatter(Locale.US);
final LocalDateTime parsedDateTime = LocalDateTime.parse(date, parser);
return Date.from(parsedDateTime.toInstant(ZONE_ID.getRules().getOffset(now)));
} }
} }