Search in sources :

Example 96 with DateTimeFormatter

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);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 97 with DateTimeFormatter

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);
}
Also used : DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 98 with DateTimeFormatter

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());
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) OffsetDateTime(java.time.OffsetDateTime) OffsetTime(java.time.OffsetTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 99 with DateTimeFormatter

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);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 100 with DateTimeFormatter

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)));
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder)

Aggregations

DateTimeFormatter (java.time.format.DateTimeFormatter)905 Test (org.junit.Test)297 Test (org.testng.annotations.Test)272 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)229 TemporalAccessor (java.time.temporal.TemporalAccessor)205 LocalDate (java.time.LocalDate)142 ParsePosition (java.text.ParsePosition)94 LocalDateTime (java.time.LocalDateTime)92 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)85 ZonedDateTime (java.time.ZonedDateTime)71 Instant (java.time.Instant)53 DateTimeParseException (java.time.format.DateTimeParseException)50 ZoneId (java.time.ZoneId)39 TemporalField (java.time.temporal.TemporalField)39 WeekFields (java.time.temporal.WeekFields)36 AbstractTCKTest (tck.java.time.AbstractTCKTest)36 DateTimeException (java.time.DateTimeException)34 IOException (java.io.IOException)26 Format (java.text.Format)26 LocalTime (java.time.LocalTime)25