Package java.time.format

Examples of java.time.format.DateTimeFormatter


  }


  @Override
  public TemporalAccessor parse(String text, Locale locale) throws ParseException {
    DateTimeFormatter formatterToUse = DateTimeContextHolder.getFormatter(this.formatter, locale);
    if (LocalDate.class.equals(this.temporalAccessorType)) {
      return LocalDate.parse(text, formatterToUse);
    }
    else if (LocalTime.class.equals(this.temporalAccessorType)) {
      return LocalTime.parse(text, formatterToUse);
View Full Code Here


   * @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)) {
      dateTimeFormatter = DateTimeFormatter.ofPattern(this.pattern);
    }
    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);
  }
View Full Code Here

   * (generally user independent)
   * @param locale the current user locale (may be {@code null} if not known)
   * @return the user-specific DateTimeFormatter
   */
  public static DateTimeFormatter getFormatter(DateTimeFormatter formatter, Locale locale) {
    DateTimeFormatter formatterToUse = (locale != null ? formatter.withLocale(locale) : formatter);
    DateTimeContext context = getDateTimeContext();
    return (context != null ? context.getFormatter(formatterToUse) : formatterToUse);
  }
View Full Code Here

    return FIELD_TYPES;
  }

  @Override
  public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
    DateTimeFormatter formatter = getFormatter(annotation, fieldType);
    return new TemporalAccessorPrinter(formatter);
  }
View Full Code Here

  }

  @Override
  @SuppressWarnings("unchecked")
  public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
    DateTimeFormatter formatter = getFormatter(annotation, fieldType);
    return new TemporalAccessorParser((Class<? extends TemporalAccessor>) fieldType, formatter);
  }
View Full Code Here

  @Override
  public void registerFormatters(FormatterRegistry registry) {
    DateTimeConverters.registerConverters(registry);

    DateTimeFormatter dateFormatter = getFormatter(Type.DATE);
    DateTimeFormatter timeFormatter = getFormatter(Type.TIME);
    DateTimeFormatter dateTimeFormatter = getFormatter(Type.DATE_TIME);

    registry.addFormatterForFieldType(LocalDate.class,
        new TemporalAccessorPrinter(dateFormatter),
        new TemporalAccessorParser(LocalDate.class, dateFormatter));
View Full Code Here

    registry.addFormatterForFieldAnnotation(new Jsr310DateTimeFormatAnnotationFormatterFactory());
  }

  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);
  }
View Full Code Here

  }

  @Test
  public void createDateTimeFormatterWithPattern() throws Exception {
    factory = new DateTimeFormatterFactory("yyyyMMddHHmmss");
    DateTimeFormatter formatter = factory.createDateTimeFormatter();
    assertThat(formatter.format(dateTime), is("20091021121000"));
  }
View Full Code Here

    assertThat(formatter.format(dateTime), is("20091021121000"));
  }

  @Test
  public void createDateTimeFormatterWithNullFallback() throws Exception {
    DateTimeFormatter formatter = factory.createDateTimeFormatter(null);
    assertThat(formatter, is(nullValue()));
  }
View Full Code Here

    assertThat(formatter, is(nullValue()));
  }

  @Test
  public void createDateTimeFormatterWithFallback() throws Exception {
    DateTimeFormatter fallback = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
    DateTimeFormatter formatter = factory.createDateTimeFormatter(fallback);
    assertThat(formatter, is(sameInstance(fallback)));
  }
View Full Code Here

TOP

Related Classes of java.time.format.DateTimeFormatter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.