use of java.time.format.DateTimeFormatter in project spring-framework by spring-projects.
the class DateTimeFormatterFactory method createDateTimeFormatter.
/**
* Create a new {@code DateTimeFormatter} using this factory.
* <p>If no specific pattern or style has been defined,
* the supplied {@code fallbackFormatter} will be used.
* @param fallbackFormatter the fall-back formatter to use when no specific
* factory properties have been set (can be {@code null}).
* @return a new date time formatter
*/
public DateTimeFormatter createDateTimeFormatter(DateTimeFormatter fallbackFormatter) {
DateTimeFormatter dateTimeFormatter = null;
if (StringUtils.hasLength(this.pattern)) {
// Using strict parsing to align with Joda-Time and standard DateFormat behavior:
// otherwise, an overflow like e.g. Feb 29 for a non-leap-year wouldn't get rejected.
// However, with strict parsing, a year digit needs to be specified as 'u'...
String patternToUse = this.pattern.replace("yy", "uu");
dateTimeFormatter = DateTimeFormatter.ofPattern(patternToUse).withResolverStyle(ResolverStyle.STRICT);
} else if (this.iso != null && this.iso != ISO.NONE) {
switch(this.iso) {
case DATE:
dateTimeFormatter = DateTimeFormatter.ISO_DATE;
break;
case TIME:
dateTimeFormatter = DateTimeFormatter.ISO_TIME;
break;
case DATE_TIME:
dateTimeFormatter = DateTimeFormatter.ISO_DATE_TIME;
break;
case NONE:
/* no-op */
break;
default:
throw new IllegalStateException("Unsupported ISO format: " + this.iso);
}
} else if (this.dateStyle != null && this.timeStyle != null) {
dateTimeFormatter = DateTimeFormatter.ofLocalizedDateTime(this.dateStyle, this.timeStyle);
} else if (this.dateStyle != null) {
dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(this.dateStyle);
} else if (this.timeStyle != null) {
dateTimeFormatter = DateTimeFormatter.ofLocalizedTime(this.timeStyle);
}
if (dateTimeFormatter != null && this.timeZone != null) {
dateTimeFormatter = dateTimeFormatter.withZone(this.timeZone.toZoneId());
}
return (dateTimeFormatter != null ? dateTimeFormatter : fallbackFormatter);
}
use of java.time.format.DateTimeFormatter in project spring-framework by spring-projects.
the class DateTimeFormatterRegistrar method getFormatter.
private DateTimeFormatter getFormatter(Type type) {
DateTimeFormatter formatter = this.formatters.get(type);
if (formatter != null) {
return formatter;
}
DateTimeFormatter fallbackFormatter = getFallbackFormatter(type);
return this.factories.get(type).createDateTimeFormatter(fallbackFormatter);
}
use of java.time.format.DateTimeFormatter in project spring-framework by spring-projects.
the class DateTimeFormatterRegistrar method registerFormatters.
@Override
public void registerFormatters(FormatterRegistry registry) {
DateTimeConverters.registerConverters(registry);
DateTimeFormatter df = getFormatter(Type.DATE);
DateTimeFormatter tf = getFormatter(Type.TIME);
DateTimeFormatter dtf = getFormatter(Type.DATE_TIME);
// Efficient ISO_LOCAL_* variants for printing since they are twice as fast...
registry.addFormatterForFieldType(LocalDate.class, new TemporalAccessorPrinter(df == DateTimeFormatter.ISO_DATE ? DateTimeFormatter.ISO_LOCAL_DATE : df), new TemporalAccessorParser(LocalDate.class, df));
registry.addFormatterForFieldType(LocalTime.class, new TemporalAccessorPrinter(tf == DateTimeFormatter.ISO_TIME ? DateTimeFormatter.ISO_LOCAL_TIME : tf), new TemporalAccessorParser(LocalTime.class, tf));
registry.addFormatterForFieldType(LocalDateTime.class, new TemporalAccessorPrinter(dtf == DateTimeFormatter.ISO_DATE_TIME ? DateTimeFormatter.ISO_LOCAL_DATE_TIME : dtf), new TemporalAccessorParser(LocalDateTime.class, dtf));
registry.addFormatterForFieldType(ZonedDateTime.class, new TemporalAccessorPrinter(dtf), new TemporalAccessorParser(ZonedDateTime.class, dtf));
registry.addFormatterForFieldType(OffsetDateTime.class, new TemporalAccessorPrinter(dtf), new TemporalAccessorParser(OffsetDateTime.class, dtf));
registry.addFormatterForFieldType(OffsetTime.class, new TemporalAccessorPrinter(tf), new TemporalAccessorParser(OffsetTime.class, tf));
registry.addFormatterForFieldType(Instant.class, new InstantFormatter());
registry.addFormatterForFieldType(Period.class, new PeriodFormatter());
registry.addFormatterForFieldType(Duration.class, new DurationFormatter());
registry.addFormatterForFieldType(YearMonth.class, new YearMonthFormatter());
registry.addFormatterForFieldType(MonthDay.class, new MonthDayFormatter());
registry.addFormatterForFieldAnnotation(new Jsr310DateTimeFormatAnnotationFormatterFactory());
}
use of java.time.format.DateTimeFormatter in project nifi by apache.
the class TimeAdapter method marshal.
@Override
public String marshal(Date date) throws Exception {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT, Locale.US);
final ZonedDateTime localDateTime = ZonedDateTime.ofInstant(date.toInstant(), ZONE_ID);
return formatter.format(localDateTime);
}
use of java.time.format.DateTimeFormatter in project nifi by apache.
the class TimeAdapter method unmarshal.
@Override
public Date unmarshal(String date) throws Exception {
final LocalDateTime now = LocalDateTime.now();
final DateTimeFormatter parser = new DateTimeFormatterBuilder().appendPattern(DEFAULT_TIME_FORMAT).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)));
}
Aggregations