/** * <p>Constructs a new FastDateFormat.</p> * * @param pattern {@link java.text.SimpleDateFormat} compatible pattern * @param timeZone non-null time zone to use * @param locale non-null locale to use * @param centuryStart The start of the 100 year period to use as the "default century" for 2 digit year parsing. If centuryStart is null, defaults to now - 80 years * @throws NullPointerException if pattern, timeZone, or locale is null. */ protected FastDateFormat(final String pattern, final TimeZone timeZone, final Locale locale, final Date centuryStart) { printer= new FastDatePrinter(pattern, timeZone, locale); parser= new FastDateParser(pattern, timeZone, locale, centuryStart); }
/** * <p>Formats a date/time into a specific pattern using the UTC time zone.</p> * * @param date the date to format, not null * @param pattern the pattern to use to format the date, not null * @return the formatted date */ public static String formatUTC(final Date date, final String pattern) { return format(date, pattern, UTC_TIME_ZONE, null); }
@Override protected FastDateFormat createInstance(final String pattern, final TimeZone timeZone, final Locale locale) { return new FastDateFormat(pattern, timeZone, locale); } };
@Override protected DatePrinter getInstance(final String format, final TimeZone timeZone, final Locale locale) { return FastDateFormat.getInstance(format, timeZone, locale); } }
/** * <p>Formats the time gap as a string.</p> * * <p>The format used is ISO 8601-like: {@code HH:mm:ss.SSS}.</p> * * @param durationMillis the duration to format * @return the formatted duration, not null * @throws java.lang.IllegalArgumentException if durationMillis is negative */ public static String formatDurationHMS(final long durationMillis) { return formatDuration(durationMillis, "HH:mm:ss.SSS"); }
private void testUTC(final String expectedValue, final String pattern) { final TimeZone timeZone = FastTimeZone.getGmtTimeZone(); assertFormats(expectedValue, pattern, timeZone, createFebruaryTestDate(timeZone)); }
/** * Adds a number of months to a date returning a new object. * The original {@code Date} is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new {@code Date} with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addMonths(final Date date, final int amount) { return add(date, Calendar.MONTH, amount); }
/** * <p>Formats a millisecond {@code long} value.</p> * * @param millis the millisecond value to format * @return the formatted string * @since 2.1 */ @Override public String format(final long millis) { return printer.format(millis); }
/** * Override this method in derived tests to change the construction of instances * * @param format the format string to use * @param timeZone the time zone to use * @param locale the locale to use * * @return the DateParser instance to use for testing */ protected DateParser getInstance(final String format, final TimeZone timeZone, final Locale locale) { return new FastDateParser(format, timeZone, locale, null); }
/** * <p>Gets a formatter instance using the default pattern in the * default locale.</p> * * @return a date/time formatter */ public static FastDateFormat getInstance() { return cache.getInstance(); }
/** * <p>Gets the pattern used by this formatter.</p> * * @return the pattern, {@link java.text.SimpleDateFormat} compatible */ @Override public String getPattern() { return printer.getPattern(); }
/** * <p>Gets the time zone used by this formatter.</p> * * <p>This zone is always used for {@code Date} formatting. </p> * * @return the time zone */ @Override public TimeZone getTimeZone() { return printer.getTimeZone(); }
/** * <p>Gets the locale used by this formatter.</p> * * @return the locale */ @Override public Locale getLocale() { return printer.getLocale(); }
/** * {@inheritDoc} */ @Override public int estimateLength() { return mRule.estimateLength(); }
@Override public void appendTo(final Appendable buffer, final int value) throws IOException { mRule.appendTo(buffer, value); } }
/** * <p>Formats a calendar into a specific pattern.</p> * * @param calendar the calendar to format, not null * @param pattern the pattern to use to format the calendar, not null * @return the formatted calendar * @see FastDateFormat#format(Calendar) * @since 2.4 */ public static String format(final Calendar calendar, final String pattern) { return format(calendar, pattern, null, null); }
/** * Adds a number of days to a date returning a new object. * The original {@code Date} is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new {@code Date} with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addDays(final Date date, final int amount) { return add(date, Calendar.DAY_OF_MONTH, amount); }
/** * <p>Gets a formatter instance using the specified pattern in the * default locale.</p> * * @param pattern {@link java.text.SimpleDateFormat} compatible * pattern * @return a pattern based date/time formatter * @throws IllegalArgumentException if pattern is invalid */ public static FastDateFormat getInstance(final String pattern) { return cache.getInstance(pattern, null, null); }
/** * Adds a number of minutes to a date returning a new object. * The original {@code Date} is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new {@code Date} with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addMinutes(final Date date, final int amount) { return add(date, Calendar.MINUTE, amount); }
/** * Adds a number of seconds to a date returning a new object. * The original {@code Date} is unchanged. * * @param date the date, not null * @param amount the amount to add, may be negative * @return the new {@code Date} with the amount added * @throws IllegalArgumentException if the date is null */ public static Date addSeconds(final Date date, final int amount) { return add(date, Calendar.SECOND, amount); }