Package com.irurueta.units
Class TimeFormatter
- All Implemented Interfaces:
Cloneable
Formats and parses time value and unit.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final StringDefault pattern to format centuriesprivate static final StringPattern to parse n-th century format.static final Stringn-th century symbol.private PatternPattern to parse n-th century format.private static final StringPattern to parse day format.static final StringDay symbol.private PatternPattern to parse day format.private static final StringPattern to parse 1st century format.private static final StringFirst century symbol.private PatternPattern to parse 1st century format.static final intFlag indicating that all units must be included when formatting a time instance.static final intFlag indicating whether centuries must be included when formatting a time instance.static final intFlag indicating that all date units (greater than hours) must be used when formatting a time instance.static final intFlag indicating that standard date units (days, months and years) must be used when formatting a time instance.static final intFlag indicating whether days must be included when formatting a time instance.static final intFlag indicating whether hours must be included when formatting a time instance.static final intFlag indicating whether microseconds must be included when formatting a time instance.static final intFlag indicating whether milliseconds must be included when formatting a time instance.static final intFlag indicating whether minutes must be included when formatting a time instance.static final intFlag indicating whether months must be included when formatting a time instance.static final intFlag indicating whether nanoseconds must be included when formatting a time instance.static final intFlag indicating whether seconds must be included when formatting a time instance.static final intFlag indicating that all time units (smaller than a day) must be used when formatting a time instance.static final intFlag indicating that standard time units (hours, minutes and seconds) must be used when formatting a time instance.static final intFlag indicating whether weeks must be included when formatting a time instance.static final intFlag indicating whether years must be included when formatting a time instance.private static final StringPattern to format hour and minutes (hh:mm.s).private static final StringPattern to format hour, minutes and seconds (hh:mm:ss.ms).private static final StringPattern to parse hour format.static final StringHour symbol.private PatternPattern to parse time in hour and minute format (hh:mm.s)private PatternPattern to parse time in hour, minute and second format (hh:mm:ss.ms).private PatternPattern to parse hour format.private static final intMinimum number of digits for hours and minutes.private static final StringPattern to parse microsecond format.static final StringMicrosecond symbol.private PatternPattern to parse microsecond format.private static final StringPattern to parse millisecond format.static final StringMillisecond symbol.private PatternPattern to parse millisecond format.private static final StringPattern to parse minute format.static final StringMinute symbol.private PatternPattern to parse minute format.private static final StringPattern to parse month format.static final StringMonth symbol.private PatternPattern to parse month format.private static final StringPattern to parse nanosecond format.static final StringNanosecond symbol.private PatternPattern to parse nanosecond format.private static final StringPattern to parse 2nd century format.private static final StringSecond century symbol.private static final StringPattern to parse second format.static final StringSecond symbol.private PatternPattern to parse 2nd century format.private PatternPattern to parse second format.private static final StringDefines a space.private static final StringPattern to parse 3rd century format.private static final StringThird century symbol.private PatternPattern to parse 3rd century format.private static final StringPattern to parse week format.static final StringWeek symbol.private PatternPattern to parse week format.private static final StringPattern to parse year format.static final StringYear symbol.private PatternPattern to parse year format.Fields inherited from class com.irurueta.units.MeasureFormatter
DEFAULT_VALUE_AND_UNIT_FORMAT_PATTERN, numberFormat -
Constructor Summary
ConstructorsConstructorDescriptionConstructor.TimeFormatter(TimeFormatter formatter) Copy constructor.TimeFormatter(Locale locale) Constructor with locale. -
Method Summary
Modifier and TypeMethodDescriptionprivate StringBuilderappendSpaceIfNeeded(StringBuilder builder) Appends a space if builder is not empty.booleanDetermines if two time formatters are equal by comparing all of its internal parameters.Attempts to determine a time unit within a measurement string representation.Formats provided time value and unit into a string representation.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.formatAndConvert(Number value, TimeUnit unit, UnitSystem system) Formats and converts provided time value and unit.formatHourMinute(Time time) Formats time instance using hour and minute format (hh:mm.ms).formatHourMinuteSecond(Time time) Formats this instance using hour, minute and second format (hh:mm::ss.ms).formatMultiple(Time time, int flags) Formats provided time instance using required units as flags.formatMultiple(Time time, int flags, boolean onlyNonZero) Formats provided time instance using required units as flags.getUnitSymbol(TimeUnit unit) Returns unit string representation.Returns unit system this instance will use based on its assigned locale.getUnitSystem(String source) Gets unit system for detected unit into provided string representation of a measurement.inthashCode()Hash code generated for this instance.Parses provided string and tries to determine a time value and unit.private Timeparse1stCentury(CharSequence source) Parses string as 1st century format.private Timeparse2ndCentury(CharSequence source) Parses string as 2nd century format.private Timeparse3rdCentury(CharSequence source) Parses string as 3rd century format.private TimeparseCentury(CharSequence source) Parses string as n-th century format.private TimeparseDay(CharSequence source) Parses string as day format.private TimeparseHour(CharSequence source) Parses string as hour format.parseHourMinute(CharSequence source) Parses provided string representation using hour and minute format (hh:mm).parseHourMinuteSecond(CharSequence source) Parses provided string representation using hour, minute and second format (hh:mm:ss).private TimeparseMicrosecond(CharSequence source) Parses string as microsecond format.private TimeparseMillisecond(CharSequence source) Parses string as millisecond format.private TimeparseMinute(CharSequence source) Parses string as minute format.private TimeparseMonth(CharSequence source) Parses string as month format.parseMultiple(CharSequence source) Parses a string containing multiple units and returns the summation of all found values as a single Time instance.private TimeparseNanosecond(CharSequence source) Parses string as nanosecond format.private TimeparseSecond(CharSequence source) Parses string as second format.private TimeparseWeek(CharSequence source) Parses string as week format.private TimeparseYear(CharSequence source) Parses string as year format.Methods inherited from class com.irurueta.units.MeasureFormatter
clone, format, format, format, format, formatAndConvert, formatAndConvert, formatAndConvert, formatAndConvert, formatAndConvert, getAvailableLocales, getLocale, getMaximumFractionDigits, getMaximumIntegerDigits, getMinimumFractionDigits, getMinimumIntegerDigits, getRoundingMode, getValueAndUnitFormatPattern, internalClone, internalParse, isGroupingUsed, isImperialUnit, isMetricUnit, isParseIntegerOnly, isValidMeasurement, isValidUnit, setGroupingUsed, setMaximumFractionDigits, setMaximumIntegerDigits, setMinimumFractionDigits, setMinimumIntegerDigits, setParseIntegerOnly, setRoundingMode, setValueAndUnitFormatPattern
-
Field Details
-
FORMAT_NANOSECONDS
public static final int FORMAT_NANOSECONDSFlag indicating whether nanoseconds must be included when formatting a time instance.- See Also:
-
FORMAT_MICROSECONDS
public static final int FORMAT_MICROSECONDSFlag indicating whether microseconds must be included when formatting a time instance.- See Also:
-
FORMAT_MILLISECONDS
public static final int FORMAT_MILLISECONDSFlag indicating whether milliseconds must be included when formatting a time instance.- See Also:
-
FORMAT_SECONDS
public static final int FORMAT_SECONDSFlag indicating whether seconds must be included when formatting a time instance.- See Also:
-
FORMAT_MINUTES
public static final int FORMAT_MINUTESFlag indicating whether minutes must be included when formatting a time instance.- See Also:
-
FORMAT_HOURS
public static final int FORMAT_HOURSFlag indicating whether hours must be included when formatting a time instance.- See Also:
-
FORMAT_DAYS
public static final int FORMAT_DAYSFlag indicating whether days must be included when formatting a time instance.- See Also:
-
FORMAT_WEEKS
public static final int FORMAT_WEEKSFlag indicating whether weeks must be included when formatting a time instance.- See Also:
-
FORMAT_MONTHS
public static final int FORMAT_MONTHSFlag indicating whether months must be included when formatting a time instance.- See Also:
-
FORMAT_YEARS
public static final int FORMAT_YEARSFlag indicating whether years must be included when formatting a time instance.- See Also:
-
FORMAT_CENTURIES
public static final int FORMAT_CENTURIESFlag indicating whether centuries must be included when formatting a time instance.- See Also:
-
FORMAT_ALL
public static final int FORMAT_ALLFlag indicating that all units must be included when formatting a time instance.- See Also:
-
FORMAT_TIME_ALL
public static final int FORMAT_TIME_ALLFlag 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_STANDARDFlag 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_ALLFlag 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_STANDARDFlag indicating that standard date units (days, months and years) must be used when formatting a time instance.- See Also:
-
NANOSECOND_SYMBOL
Nanosecond symbol.- See Also:
-
MICROSECOND_SYMBOL
Microsecond symbol.- See Also:
-
MILLISECOND_SYMBOL
Millisecond symbol.- See Also:
-
SECOND_SYMBOL
Second symbol.- See Also:
-
MINUTE_SYMBOL
Minute symbol.- See Also:
-
HOUR_SYMBOL
Hour symbol.- See Also:
-
DAY_SYMBOL
Day symbol.- See Also:
-
WEEK_SYMBOL
Week symbol.- See Also:
-
MONTH_SYMBOL
Month symbol.- See Also:
-
YEAR_SYMBOL
Year symbol.- See Also:
-
CENTURY_SYMBOL
n-th century symbol.- See Also:
-
FIRST_CENTURY_SYMBOL
First century symbol.- See Also:
-
SECOND_CENTURY_SYMBOL
Second century symbol.- See Also:
-
THIRD_CENTURY_SIMBOL
Third century symbol.- See Also:
-
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_DIGITSMinimum number of digits for hours and minutes.- See Also:
-
HOUR_MINUTE_PATTERN
Pattern to format hour and minutes (hh:mm.s).- See Also:
-
HOUR_MINUTE_SECOND_PATTERN
Pattern to format hour, minutes and seconds (hh:mm:ss.ms).- See Also:
-
FIRST_CENTURY_PATTERN
Pattern to parse 1st century format.- See Also:
-
SECOND_CENTURY_PATTERN
Pattern to parse 2nd century format.- See Also:
-
THIRD_CENTURY_PATTERN
Pattern to parse 3rd century format.- See Also:
-
CENTURY_PATTERN
Pattern to parse n-th century format.- See Also:
-
YEAR_PATTERN
Pattern to parse year format.- See Also:
-
MONTH_PATTERN
Pattern to parse month format.- See Also:
-
WEEK_PATTERN
Pattern to parse week format.- See Also:
-
DAY_PATTERN
Pattern to parse day format.- See Also:
-
HOUR_PATTERN
Pattern to parse hour format.- See Also:
-
MINUTE_PATTERN
Pattern to parse minute format.- See Also:
-
SECOND_PATTERN
Pattern to parse second format.- See Also:
-
MILLISECOND_PATTERN
Pattern to parse millisecond format.- See Also:
-
MICROSECOND_PATTERN
Pattern to parse microsecond format.- See Also:
-
NANOSECOND_PATTERN
Pattern to parse nanosecond format.- See Also:
-
SPACE
Defines a space.- See Also:
-
hourMinutePattern
Pattern to parse time in hour and minute format (hh:mm.s) -
hourMinuteSecondPattern
Pattern to parse time in hour, minute and second format (hh:mm:ss.ms). -
firstCenturyPattern
Pattern to parse 1st century format. -
secondCenturyPattern
Pattern to parse 2nd century format. -
thirdCenturyPattern
Pattern to parse 3rd century format. -
centuryPattern
Pattern to parse n-th century format. -
yearPattern
Pattern to parse year format. -
monthPattern
Pattern to parse month format. -
weekPattern
Pattern to parse week format. -
dayPattern
Pattern to parse day format. -
hourPattern
Pattern to parse hour format. -
minutePattern
Pattern to parse minute format. -
secondPattern
Pattern to parse second format. -
millisecondPattern
Pattern to parse millisecond format. -
microsecondPattern
Pattern to parse microsecond format. -
nanosecondPattern
Pattern to parse nanosecond format.
-
-
Constructor Details
-
TimeFormatter
public TimeFormatter()Constructor. -
TimeFormatter
Constructor with locale.- Parameters:
locale- locale.- Throws:
IllegalArgumentException- if locale is null.
-
TimeFormatter
Copy constructor.- Parameters:
formatter- input instance to copy from.- Throws:
NullPointerException- if provided formatter is null.
-
-
Method Details
-
equals
Determines if two time formatters are equal by comparing all of its internal parameters.- Overrides:
equalsin classMeasureFormatter<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:
hashCodein classMeasureFormatter<Time,TimeUnit> - Returns:
- hash code.
-
getUnitSystem
Gets unit system for detected unit into provided string representation of a measurement.- Specified by:
getUnitSystemin classMeasureFormatter<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
Formats provided time value and unit into a string representation.- Overrides:
formatin classMeasureFormatter<Time,TimeUnit> - Parameters:
value- a time value.unit- a time unit.- Returns:
- string representation of provided measurement value and unit.
-
format
Formats provided time value and unit into a string representation and appends the result into provided string buffer.- Overrides:
formatin classMeasureFormatter<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
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:
formatAndConvertin classMeasureFormatter<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
Returns unit system this instance will use based on its assigned locale.- Overrides:
getUnitSystemin classMeasureFormatter<Time,TimeUnit> - Returns:
- always returns metric system
-
parse
Parses provided string and tries to determine a time value and unit.- Specified by:
parsein classMeasureFormatter<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
Attempts to determine a time unit within a measurement string representation.- Specified by:
findUnitin classMeasureFormatter<Time,TimeUnit> - Parameters:
source- a measurement string representation.- Returns:
- a time unit, or null if none can be determined.
-
getUnitSymbol
Returns unit string representation.- Specified by:
getUnitSymbolin classMeasureFormatter<Time,TimeUnit> - Parameters:
unit- a measure unit.- Returns:
- its string representation.
-
formatHourMinute
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Appends a space if builder is not empty.- Parameters:
builder- builder to add space to.- Returns:
- same instance provided as parameter.
-