示例#1
0
 /**
  * Prints a date-time object to an {@code Appendable} using this formatter.
  *
  * <p>This prints the date-time to the specified destination. {@link Appendable} is a general
  * purpose interface that is implemented by all key character output classes including {@code
  * StringBuffer}, {@code StringBuilder}, {@code PrintStream} and {@code Writer}.
  *
  * <p>Although {@code Appendable} methods throw an {@code IOException}, this method does not.
  * Instead, any {@code IOException} is wrapped in a runtime exception. See {@link
  * DateTimePrintException#rethrowIOException()} for a means to extract the {@code IOException}.
  *
  * @param dateTime the date-time object to print, not null
  * @param appendable the appendable to print to, not null
  * @throws DateTimeException if an error occurs during printing
  */
 public void printTo(DateTimeAccessor dateTime, Appendable appendable) {
   Objects.requireNonNull(dateTime, "dateTime");
   Objects.requireNonNull(appendable, "appendable");
   try {
     DateTimePrintContext context = new DateTimePrintContext(dateTime, locale, symbols);
     if (appendable instanceof StringBuilder) {
       printerParser.print(context, (StringBuilder) appendable);
     } else {
       // buffer output to avoid writing to appendable in case of error
       StringBuilder buf = new StringBuilder(32);
       printerParser.print(context, buf);
       appendable.append(buf);
     }
   } catch (IOException ex) {
     throw new DateTimePrintException(ex.getMessage(), ex);
   }
 }
示例#2
0
 /**
  * Parses the text to a builder.
  *
  * <p>This parses to a {@code DateTimeBuilder} but does not require the input to be fully parsed.
  *
  * <p>This method does not throw {@link DateTimeParseException}. Instead, errors are returned
  * within the state of the specified parse position. Callers must check for errors before using
  * the context.
  *
  * <p>This method may throw some other {@code DateTimeException} if a date/time problem occurs.
  *
  * @param text the text to parse, not null
  * @param position the position to parse from, updated with length parsed and the index of any
  *     error, not null
  * @return the parsed text, null only if the parse results in an error
  * @throws IndexOutOfBoundsException if the position is invalid
  * @throws DateTimeParseException if the parse fails
  * @throws DateTimeException if there is a date/time problem
  */
 public DateTimeBuilder parseToBuilder(CharSequence text, ParsePosition position) {
   Objects.requireNonNull(text, "text");
   Objects.requireNonNull(position, "position");
   DateTimeParseContext context = new DateTimeParseContext(locale, symbols);
   int pos = position.getIndex();
   pos = printerParser.parse(context, text, pos);
   if (pos < 0) {
     position.setErrorIndex(~pos);
     return null;
   }
   position.setIndex(pos);
   return context.toBuilder();
 }
示例#3
0
 /**
  * Returns a description of the underlying formatters.
  *
  * @return the pattern that will be used, not null
  */
 @Override
 public String toString() {
   String pattern = printerParser.toString();
   return pattern.startsWith("[") ? pattern : pattern.substring(1, pattern.length() - 1);
 }
示例#4
0
 /**
  * Returns the formatter as a composite printer parser.
  *
  * @param optional whether the printer/parser should be optional
  * @return the printer/parser, not null
  */
 CompositePrinterParser toPrinterParser(boolean optional) {
   return printerParser.withOptional(optional);
 }