import java.time.Instant;
import java.time.LocalDateTime;
-import java.time.ZoneOffset;
+import java.time.ZoneId;
import java.util.Date;
import java.util.Objects;
* @author Vaadin Ltd
* @since 8.0
*/
-public class LocalDateTimeToDateConverter
- implements Converter<LocalDateTime, Date> {
+public class LocalDateTimeToDateConverter implements Converter<LocalDateTime, Date> {
- private ZoneOffset zoneOffset;
+ private ZoneId zoneId;
/**
* Creates a new converter using the given time zone.
*
- * @param zoneOffset
- * the time zone offset to use, not <code>null</code>
+ * @param zoneId the time zone to use, not <code>null</code>
*/
- public LocalDateTimeToDateConverter(ZoneOffset zoneOffset) {
- this.zoneOffset = Objects.requireNonNull(zoneOffset,
- "Zone offset cannot be null");
+ public LocalDateTimeToDateConverter(ZoneId zoneId) {
+ this.zoneId = Objects.requireNonNull(zoneId, "Zone identifier cannot be null");
}
@Override
- public Result<Date> convertToModel(LocalDateTime localDate,
- ValueContext context) {
+ public Result<Date> convertToModel(LocalDateTime localDate, ValueContext context) {
if (localDate == null) {
return Result.ok(null);
}
- return Result.ok(Date.from(localDate.toInstant(zoneOffset)));
+ return Result.ok(Date.from(localDate.atZone(zoneId).toInstant()));
}
@Override
- public LocalDateTime convertToPresentation(Date date,
- ValueContext context) {
+ public LocalDateTime convertToPresentation(Date date, ValueContext context) {
if (date == null) {
return null;
}
- return Instant.ofEpochMilli(date.getTime()).atZone(zoneOffset)
- .toLocalDateTime();
+ return Instant.ofEpochMilli(date.getTime()).atZone(zoneId).toLocalDateTime();
}
}