예제 #1
0
 @Override
 public Object parseObject(String text, ParsePosition pos) {
   Objects.requireNonNull(text, "text");
   DateTimeBuilder builder;
   try {
     builder = formatter.parseToBuilder(text, pos);
   } catch (IndexOutOfBoundsException ex) {
     if (pos.getErrorIndex() < 0) {
       pos.setErrorIndex(0);
     }
     return null;
   }
   if (builder == null) {
     if (pos.getErrorIndex() < 0) {
       pos.setErrorIndex(0);
     }
     return null;
   }
   if (parseType == null) {
     return builder;
   }
   try {
     return builder.resolve().build(parseType);
   } catch (RuntimeException ex) {
     pos.setErrorIndex(0);
     return null;
   }
 }
예제 #2
0
 /**
  * Fully parses the text producing an object of the specified type.
  *
  * <p>Most applications should use this method for parsing. It parses the entire text to produce
  * the required date-time. For example:
  *
  * <pre>
  * LocalDateTime dt = parser.parse(str, LocalDateTime.class);
  * </pre>
  *
  * If the parse completes without reading the entire length of the text, or a problem occurs
  * during parsing or merging, then an exception is thrown.
  *
  * @param <T> the type to extract
  * @param text the text to parse, not null
  * @param type the type to extract, not null
  * @return the parsed date-time, not null
  * @throws DateTimeParseException if the parse fails
  */
 public <T> T parse(CharSequence text, Class<T> type) {
   Objects.requireNonNull(text, "text");
   Objects.requireNonNull(type, "type");
   String str = text.toString(); // parsing whole String, so this makes sense
   try {
     DateTimeBuilder builder = parseToBuilder(str).resolve();
     return builder.build(type);
   } catch (DateTimeParseException ex) {
     throw ex;
   } catch (RuntimeException ex) {
     throw createError(str, ex);
   }
 }
예제 #3
0
 /**
  * Fully parses the text producing an object of one of the specified types.
  *
  * <p>This parse method is convenient for use when the parser can handle optional elements. For
  * example, a pattern of 'yyyy-MM[-dd[Z]]' can be fully parsed to an {@code OffsetDate}, or
  * partially parsed to a {@code LocalDate} or a {@code YearMonth}. The types must be specified in
  * order, starting from the best matching full-parse option and ending with the worst matching
  * minimal parse option.
  *
  * <p>The result is associated with the first type that successfully parses. Normally,
  * applications will use {@code instanceof} to check the result. For example:
  *
  * <pre>
  * DateTimeAccessor dt = parser.parseBest(str, OffsetDate.class, LocalDate.class);
  * if (dt instanceof OffsetDate) {
  *  ...
  * } else {
  *  ...
  * }
  * </pre>
  *
  * If the parse completes without reading the entire length of the text, or a problem occurs
  * during parsing or merging, then an exception is thrown.
  *
  * @param text the text to parse, not null
  * @param types the types to attempt to parse to, which must implement {@code DateTimeAccessor},
  *     not null
  * @return the parsed date-time, not null
  * @throws IllegalArgumentException if less than 2 types are specified
  * @throws DateTimeParseException if the parse fails
  */
 public DateTimeAccessor parseBest(CharSequence text, Class<?>... types) {
   Objects.requireNonNull(text, "text");
   Objects.requireNonNull(types, "types");
   if (types.length < 2) {
     throw new IllegalArgumentException("At least two types must be specified");
   }
   String str = text.toString(); // parsing whole String, so this makes sense
   try {
     DateTimeBuilder builder = parseToBuilder(str).resolve();
     for (Class<?> type : types) {
       try {
         return (DateTimeAccessor) builder.build(type);
       } catch (RuntimeException ex) {
         // continue
       }
     }
     throw new DateTimeException(
         "Unable to convert parsed text to any specified type: " + Arrays.toString(types));
   } catch (DateTimeParseException ex) {
     throw ex;
   } catch (RuntimeException ex) {
     throw createError(str, ex);
   }
 }