Class TimeFormatter

All Implemented Interfaces:
Cloneable

public class TimeFormatter extends MeasureFormatter<Time,TimeUnit>
Formats and parses time value and unit.
  • Field Details

    • FORMAT_NANOSECONDS

      public static final int FORMAT_NANOSECONDS
      Flag indicating whether nanoseconds must be included when formatting a time instance.
      See Also:
    • FORMAT_MICROSECONDS

      public static final int FORMAT_MICROSECONDS
      Flag indicating whether microseconds must be included when formatting a time instance.
      See Also:
    • FORMAT_MILLISECONDS

      public static final int FORMAT_MILLISECONDS
      Flag indicating whether milliseconds must be included when formatting a time instance.
      See Also:
    • FORMAT_SECONDS

      public static final int FORMAT_SECONDS
      Flag indicating whether seconds must be included when formatting a time instance.
      See Also:
    • FORMAT_MINUTES

      public static final int FORMAT_MINUTES
      Flag indicating whether minutes must be included when formatting a time instance.
      See Also:
    • FORMAT_HOURS

      public static final int FORMAT_HOURS
      Flag indicating whether hours must be included when formatting a time instance.
      See Also:
    • FORMAT_DAYS

      public static final int FORMAT_DAYS
      Flag indicating whether days must be included when formatting a time instance.
      See Also:
    • FORMAT_WEEKS

      public static final int FORMAT_WEEKS
      Flag indicating whether weeks must be included when formatting a time instance.
      See Also:
    • FORMAT_MONTHS

      public static final int FORMAT_MONTHS
      Flag indicating whether months must be included when formatting a time instance.
      See Also:
    • FORMAT_YEARS

      public static final int FORMAT_YEARS
      Flag indicating whether years must be included when formatting a time instance.
      See Also:
    • FORMAT_CENTURIES

      public static final int FORMAT_CENTURIES
      Flag indicating whether centuries must be included when formatting a time instance.
      See Also:
    • FORMAT_ALL

      public static final int FORMAT_ALL
      Flag indicating that all units must be included when formatting a time instance.
      See Also:
    • FORMAT_TIME_ALL

      public static final int FORMAT_TIME_ALL
      Flag indicating that all time units (smaller than a day) must be used when formatting a time instance.
      See Also:
    • FORMAT_TIME_STANDARD

      public static final int FORMAT_TIME_STANDARD
      Flag indicating that standard time units (hours, minutes and seconds) must be used when formatting a time instance.
      See Also:
    • FORMAT_DATE_ALL

      public static final int FORMAT_DATE_ALL
      Flag indicating that all date units (greater than hours) must be used when formatting a time instance.
      See Also:
    • FORMAT_DATE_STANDARD

      public static final int FORMAT_DATE_STANDARD
      Flag indicating that standard date units (days, months and years) must be used when formatting a time instance.
      See Also:
    • NANOSECOND_SYMBOL

      public static final String NANOSECOND_SYMBOL
      Nanosecond symbol.
      See Also:
    • MICROSECOND_SYMBOL

      public static final String MICROSECOND_SYMBOL
      Microsecond symbol.
      See Also:
    • MILLISECOND_SYMBOL

      public static final String MILLISECOND_SYMBOL
      Millisecond symbol.
      See Also:
    • SECOND_SYMBOL

      public static final String SECOND_SYMBOL
      Second symbol.
      See Also:
    • MINUTE_SYMBOL

      public static final String MINUTE_SYMBOL
      Minute symbol.
      See Also:
    • HOUR_SYMBOL

      public static final String HOUR_SYMBOL
      Hour symbol.
      See Also:
    • DAY_SYMBOL

      public static final String DAY_SYMBOL
      Day symbol.
      See Also:
    • WEEK_SYMBOL

      public static final String WEEK_SYMBOL
      Week symbol.
      See Also:
    • MONTH_SYMBOL

      public static final String MONTH_SYMBOL
      Month symbol.
      See Also:
    • YEAR_SYMBOL

      public static final String YEAR_SYMBOL
      Year symbol.
      See Also:
    • CENTURY_SYMBOL

      public static final String CENTURY_SYMBOL
      n-th century symbol.
      See Also:
    • FIRST_CENTURY_SYMBOL

      private static final String FIRST_CENTURY_SYMBOL
      First century symbol.
      See Also:
    • SECOND_CENTURY_SYMBOL

      private static final String SECOND_CENTURY_SYMBOL
      Second century symbol.
      See Also:
    • THIRD_CENTURY_SIMBOL

      private static final String THIRD_CENTURY_SIMBOL
      Third century symbol.
      See Also:
    • CENTURY_FORMAT_PATTERN

      private static final String CENTURY_FORMAT_PATTERN
      Default pattern to format centuries. {0} corresponds to the value, {1} corresponds to the unit part.
      See Also:
    • INTEGER_DIGITS

      private static final int INTEGER_DIGITS
      Minimum number of digits for hours and minutes.
      See Also:
    • HOUR_MINUTE_PATTERN

      private static final String HOUR_MINUTE_PATTERN
      Pattern to format hour and minutes (hh:mm.s).
      See Also:
    • HOUR_MINUTE_SECOND_PATTERN

      private static final String HOUR_MINUTE_SECOND_PATTERN
      Pattern to format hour, minutes and seconds (hh:mm:ss.ms).
      See Also:
    • FIRST_CENTURY_PATTERN

      private static final String FIRST_CENTURY_PATTERN
      Pattern to parse 1st century format.
      See Also:
    • SECOND_CENTURY_PATTERN

      private static final String SECOND_CENTURY_PATTERN
      Pattern to parse 2nd century format.
      See Also:
    • THIRD_CENTURY_PATTERN

      private static final String THIRD_CENTURY_PATTERN
      Pattern to parse 3rd century format.
      See Also:
    • CENTURY_PATTERN

      private static final String CENTURY_PATTERN
      Pattern to parse n-th century format.
      See Also:
    • YEAR_PATTERN

      private static final String YEAR_PATTERN
      Pattern to parse year format.
      See Also:
    • MONTH_PATTERN

      private static final String MONTH_PATTERN
      Pattern to parse month format.
      See Also:
    • WEEK_PATTERN

      private static final String WEEK_PATTERN
      Pattern to parse week format.
      See Also:
    • DAY_PATTERN

      private static final String DAY_PATTERN
      Pattern to parse day format.
      See Also:
    • HOUR_PATTERN

      private static final String HOUR_PATTERN
      Pattern to parse hour format.
      See Also:
    • MINUTE_PATTERN

      private static final String MINUTE_PATTERN
      Pattern to parse minute format.
      See Also:
    • SECOND_PATTERN

      private static final String SECOND_PATTERN
      Pattern to parse second format.
      See Also:
    • MILLISECOND_PATTERN

      private static final String MILLISECOND_PATTERN
      Pattern to parse millisecond format.
      See Also:
    • MICROSECOND_PATTERN

      private static final String MICROSECOND_PATTERN
      Pattern to parse microsecond format.
      See Also:
    • NANOSECOND_PATTERN

      private static final String NANOSECOND_PATTERN
      Pattern to parse nanosecond format.
      See Also:
    • SPACE

      private static final String SPACE
      Defines a space.
      See Also:
    • hourMinutePattern

      private Pattern hourMinutePattern
      Pattern to parse time in hour and minute format (hh:mm.s)
    • hourMinuteSecondPattern

      private Pattern hourMinuteSecondPattern
      Pattern to parse time in hour, minute and second format (hh:mm:ss.ms).
    • firstCenturyPattern

      private Pattern firstCenturyPattern
      Pattern to parse 1st century format.
    • secondCenturyPattern

      private Pattern secondCenturyPattern
      Pattern to parse 2nd century format.
    • thirdCenturyPattern

      private Pattern thirdCenturyPattern
      Pattern to parse 3rd century format.
    • centuryPattern

      private Pattern centuryPattern
      Pattern to parse n-th century format.
    • yearPattern

      private Pattern yearPattern
      Pattern to parse year format.
    • monthPattern

      private Pattern monthPattern
      Pattern to parse month format.
    • weekPattern

      private Pattern weekPattern
      Pattern to parse week format.
    • dayPattern

      private Pattern dayPattern
      Pattern to parse day format.
    • hourPattern

      private Pattern hourPattern
      Pattern to parse hour format.
    • minutePattern

      private Pattern minutePattern
      Pattern to parse minute format.
    • secondPattern

      private Pattern secondPattern
      Pattern to parse second format.
    • millisecondPattern

      private Pattern millisecondPattern
      Pattern to parse millisecond format.
    • microsecondPattern

      private Pattern microsecondPattern
      Pattern to parse microsecond format.
    • nanosecondPattern

      private Pattern nanosecondPattern
      Pattern to parse nanosecond format.
  • Constructor Details

    • TimeFormatter

      public TimeFormatter()
      Constructor.
    • TimeFormatter

      public TimeFormatter(Locale locale)
      Constructor with locale.
      Parameters:
      locale - locale.
      Throws:
      IllegalArgumentException - if locale is null.
    • TimeFormatter

      public TimeFormatter(TimeFormatter formatter)
      Copy constructor.
      Parameters:
      formatter - input instance to copy from.
      Throws:
      NullPointerException - if provided formatter is null.
  • Method Details

    • equals

      public boolean equals(Object obj)
      Determines if two time formatters are equal by comparing all of its internal parameters.
      Overrides:
      equals in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      obj - another object to compare.
      Returns:
      true if provided object is assumed to be equal to this instance.
    • hashCode

      public int hashCode()
      Hash code generated for this instance. Hash codes can be internally used by some collections to coarsely compare objects. This implementation only calls parent implementation to avoid static analyzer warning.
      Overrides:
      hashCode in class MeasureFormatter<Time,TimeUnit>
      Returns:
      hash code.
    • getUnitSystem

      public UnitSystem getUnitSystem(String source)
      Gets unit system for detected unit into provided string representation of a measurement.
      Specified by:
      getUnitSystem in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      source - a measurement string representation to be checked.
      Returns:
      returns metric system only for units belonging to the International System of units.
    • format

      public String format(Number value, TimeUnit unit)
      Formats provided time value and unit into a string representation.
      Overrides:
      format in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      value - a time value.
      unit - a time unit.
      Returns:
      string representation of provided measurement value and unit.
    • format

      public StringBuffer format(Number value, TimeUnit unit, StringBuffer toAppendTo, FieldPosition pos)
      Formats provided time value and unit into a string representation and appends the result into provided string buffer.
      Overrides:
      format in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      value - a time value.
      unit - a time unit.
      toAppendTo - buffer to append the result to.
      pos - field position where result will be appended.
      Returns:
      provided string buffer where result is appended.
    • formatAndConvert

      public String formatAndConvert(Number value, TimeUnit unit, UnitSystem system)
      Formats and converts provided time value and unit. Unit system is ignored since time is always expressed in metric system, but some units might not belong to the international system of units. If provided value is too large for provided unit, this method will convert it to a more appropriate unit.
      Specified by:
      formatAndConvert in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      value - a measurement value.
      unit - a measurement unit.
      system - it is ignored.
      Returns:
      a string representation of measurement value and unit.
    • getUnitSystem

      public UnitSystem getUnitSystem()
      Returns unit system this instance will use based on its assigned locale.
      Overrides:
      getUnitSystem in class MeasureFormatter<Time,TimeUnit>
      Returns:
      always returns metric system
    • parse

      public Time parse(String source) throws ParseException, UnknownUnitException
      Parses provided string and tries to determine a time value and unit.
      Specified by:
      parse in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      source - text to be parsed.
      Returns:
      time containing a value and unit.
      Throws:
      ParseException - if provided string cannot be parsed.
      UnknownUnitException - if unit cannot be determined.
    • findUnit

      public TimeUnit findUnit(String source)
      Attempts to determine a time unit within a measurement string representation.
      Specified by:
      findUnit in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      source - a measurement string representation.
      Returns:
      a time unit, or null if none can be determined.
    • getUnitSymbol

      public String getUnitSymbol(TimeUnit unit)
      Returns unit string representation.
      Specified by:
      getUnitSymbol in class MeasureFormatter<Time,TimeUnit>
      Parameters:
      unit - a measure unit.
      Returns:
      its string representation.
    • formatHourMinute

      public String formatHourMinute(Time time)
      Formats time instance using hour and minute format (hh:mm.ms).
      Parameters:
      time - time to be formatted.
      Returns:
      a string representation of provided time instance using hour and minute format (hh:mm.ms).
    • parseHourMinute

      public Time parseHourMinute(CharSequence source) throws ParseException, UnknownUnitException
      Parses provided string representation using hour and minute format (hh:mm). Note: decimals are not accepted on either hour or minute values.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time.
      Throws:
      ParseException - if parsing fails.
      UnknownUnitException - if format is not recognized.
    • formatHourMinuteSecond

      public String formatHourMinuteSecond(Time time)
      Formats this instance using hour, minute and second format (hh:mm::ss.ms).
      Parameters:
      time - time to be formatted.
      Returns:
      a string representation of provided time instance using hour, minute and second format (hh:mm:ss.ms).
    • parseHourMinuteSecond

      public Time parseHourMinuteSecond(CharSequence source) throws ParseException, UnknownUnitException
      Parses provided string representation using hour, minute and second format (hh:mm:ss). Note: decimals are not accepted on either hour, minute or seconds values
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time.
      Throws:
      ParseException - if parsing fails.
      UnknownUnitException - if format is not recognized.
    • formatMultiple

      public String formatMultiple(Time time, int flags)
      Formats provided time instance using required units as flags. Flags can be provided as bitwise combinations of FORMAT constants. Only non zero units will be included.
      Parameters:
      time - time to be formatted.
      flags - flags indicating units to include.
      Returns:
      formatted time.
    • formatMultiple

      public String formatMultiple(Time time, int flags, boolean onlyNonZero)
      Formats provided time instance using required units as flags. Flags can be provided as bitwise combinations of FORMAT constants.
      Parameters:
      time - time to be formatted.
      flags - flags indicating units to include.
      onlyNonZero - true indicates to include only non zero units, false to include all selected units even if they are zero.
      Returns:
      formatted time.
    • parseMultiple

      public Time parseMultiple(CharSequence source) throws ParseException, UnknownUnitException
      Parses a string containing multiple units and returns the summation of all found values as a single Time instance. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      summation of all time values that have been parsed.
      Throws:
      ParseException - if parsing fails.
      UnknownUnitException - if format is not recognized.
    • parse1stCentury

      private Time parse1stCentury(CharSequence source) throws ParseException
      Parses string as 1st century format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in century units.
      Throws:
      ParseException - if parsing fails.
    • parse2ndCentury

      private Time parse2ndCentury(CharSequence source) throws ParseException
      Parses string as 2nd century format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in century units.
      Throws:
      ParseException - if parsing fails.
    • parse3rdCentury

      private Time parse3rdCentury(CharSequence source) throws ParseException
      Parses string as 3rd century format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in century units.
      Throws:
      ParseException - if parsing fails.
    • parseCentury

      private Time parseCentury(CharSequence source) throws ParseException
      Parses string as n-th century format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in century units.
      Throws:
      ParseException - if parsing fails.
    • parseYear

      private Time parseYear(CharSequence source) throws ParseException
      Parses string as year format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in year units.
      Throws:
      ParseException - if parsing fails.
    • parseMonth

      private Time parseMonth(CharSequence source) throws ParseException
      Parses string as month format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in month units.
      Throws:
      ParseException - if parsing fails.
    • parseWeek

      private Time parseWeek(CharSequence source) throws ParseException
      Parses string as week format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in week units.
      Throws:
      ParseException - if parsing fails.
    • parseDay

      private Time parseDay(CharSequence source) throws ParseException
      Parses string as day format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in day units.
      Throws:
      ParseException - if parsing fails.
    • parseHour

      private Time parseHour(CharSequence source) throws ParseException
      Parses string as hour format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in hour units.
      Throws:
      ParseException - if parsing fails.
    • parseMinute

      private Time parseMinute(CharSequence source) throws ParseException
      Parses string as minute format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in minute units.
      Throws:
      ParseException - if parsing fails.
    • parseSecond

      private Time parseSecond(CharSequence source) throws ParseException
      Parses string as second format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in second units.
      Throws:
      ParseException - if parsing fails.
    • parseMillisecond

      private Time parseMillisecond(CharSequence source) throws ParseException
      Parses string as millisecond format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in millisecond units.
      Throws:
      ParseException - if parsing fails.
    • parseMicrosecond

      private Time parseMicrosecond(CharSequence source) throws ParseException
      Parses string as microsecond format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in microsecond units.
      Throws:
      ParseException - if parsing fails.
    • parseNanosecond

      private Time parseNanosecond(CharSequence source) throws ParseException
      Parses string as nanosecond format. This method does not allow decimal values with fractions or thousand separators.
      Parameters:
      source - string to be parsed.
      Returns:
      parsed time in nanosecond units.
      Throws:
      ParseException - if parsing fails.
    • appendSpaceIfNeeded

      private StringBuilder appendSpaceIfNeeded(StringBuilder builder)
      Appends a space if builder is not empty.
      Parameters:
      builder - builder to add space to.
      Returns:
      same instance provided as parameter.