com.regnosys.rosetta.translate.basic.ZonedDateTimeParseHandler Maven / Gradle / Ivy
package com.regnosys.rosetta.translate.basic;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
public class ZonedDateTimeParseHandler extends BasicParseHandler {
private DateTimeFormatter pattern;
// TODO - Make this ZoneId configurable
private static final ZoneId UTC = ZoneId.of("UTC");
public ZonedDateTimeParseHandler(boolean allowsMultiple, boolean isCondition, boolean removeHtml) {
super(allowsMultiple, isCondition, removeHtml);
this.pattern = DateTimeFormatter.ISO_ZONED_DATE_TIME;
}
public ZonedDateTimeParseHandler(boolean allowsMultiple, boolean isCondition, boolean removeHtml, String pattern) {
super(allowsMultiple, isCondition, removeHtml);
this.pattern = DateTimeFormatter.ofPattern(pattern);
}
@Override
public String toString() {
return "ZonedDateTime";
}
@Override
protected ZonedDateTime convert(String value) {
try {
return ZonedDateTime.parse(value, pattern);
} catch (DateTimeParseException e) {
return ZonedDateTime.of(LocalDateTime.parse(value), UTC);
}
}
}