@Status(stage=DEVELOPMENT, unitTests=PARTIAL) @Review(by="Kees Schotanus", at="2009-09-28") public class Formatter extends Object implements Serializable
final Formatter formatter = new Formatter(Locale.US);
final Date date = formatter.parseDate("3/14/1896");
When you have a date object, you can simply format it using: formatter.formatDate(date);
FormatterFactory
.
This date formatter in turn is then used over and over again to parse and format dates.
Naturally you can customize the way objects are parsed and formatted, for example using patterns.
Lets assume you want to parse ISO 8601 dates.
These dates are in the form yyyy-mm-dd.
You can parse these dates with the formatter declared above, using the following code:final Date date = formatter.parseDate("1896-03-14", "yyyy-MM-dd");
formatter.parseDateTime("18960314", "yyyy-MM-dd");
FormatterFactory
,
Serialized FormModifier and Type | Field and Description |
---|---|
static String |
KEY_BOOLEAN_FALSE
Key to the default date pattern in the resource bundle.
|
static String |
KEY_BOOLEAN_TRUE
Key to the default date pattern in the resource bundle.
|
static String |
MSG_DATE_INVALID
Key to lookup: Invalid date.
|
static String |
MSG_DATE_INVALID_WITH_PATTERN
Key to lookup: Invalid date.
|
static String |
MSG_DATE_INVALID_YEAR
Key to lookup: Invalid year.
|
static String |
MSG_DATE_TIME_INVALID
Key to lookup: Invalid date/time.
|
static String |
MSG_DATE_TIME_INVALID_WITH_PATTERN
Key to lookup: Invalid date/time.
|
static String |
MSG_DECIMAL_INVALID
Key to lookup: Invalid decimal number.
|
static String |
MSG_DECIMAL_INVALID_GROUPING
Key to lookup: Invalid decimal number due to invalid grouping.
|
static String |
MSG_INTEGER_INVALID
Key to lookup: Invalid integer number.
|
static String |
MSG_INTEGER_OUT_OF_RANGE
Key to lookup: Integer out of range.
|
static String |
MSG_LONG_INVALID
Key to lookup: Invalid long number.
|
static String |
MSG_LONG_OUT_OF_RANGE
Key to lookup: Long number out of range.
|
static String |
MSG_TIME_INVALID
Key to lookup: Invalid time.
|
static String |
MSG_TIME_INVALID_WITH_PATTERN
Key to lookup: Invalid time.
|
Constructor and Description |
---|
Formatter(Locale locale)
Constructs a formatter using the supplied locale.
|
Modifier and Type | Method and Description |
---|---|
static String |
format(Locale locale,
String messagePattern,
Object... messageArguments)
Formats the supplied messagePattern with the optionally supplied messageArguments.
|
static String |
format(Object input)
Formats the supplied input object using the standard toString() method of the input object.
|
static String |
format(Object input,
String defaultValue)
Formats the supplied input object using the standard toString() method of the input object.
|
String |
formatBoolean(Boolean value)
Formats the supplied Boolean value.
|
String |
formatDate(Date date)
Formats the supplied date using a cached formatter or in the absence of such a formatter with a newly created formatter
that will be cached.
|
String |
formatDate(Date date,
int style)
Formats the supplied date using the supplied style.
|
String |
formatDate(Date date,
String pattern)
Formats the supplied date using the supplied pattern.
|
String |
formatDateTime(Date dateTime)
Formats the supplied dateTime using a cached formatter or in the absence of such a formatter with a newly created
formatter that will be cached.
|
String |
formatDateTime(Date dateTime,
int dateStyle,
int timeStyle)
Formats the supplied dateTime using the supplied style.
|
String |
formatDateTime(Date dateTime,
String pattern)
Formats the supplied dateTime using the supplied pattern.
|
String |
formatDecimal(Number number)
Formats the supplied decimal using a cached formatter or in the absence of such a formatter with a newly created
formatter that will be cached.
|
String |
formatDecimal(Number number,
int decimals)
Formats the supplied number using the supplied number of decimals.
|
String |
formatDecimal(Number number,
String pattern)
Formats the supplied number using the supplied pattern.
|
String |
formatInteger(Number number)
Formats the supplied number using a cached formatter or in the absence of such a formatter with a newly created
formatter that will be cached.
|
String |
formatInteger(Number number,
String pattern)
Formats the supplied number using the supplied pattern.
|
String |
formatTime(Date time)
Formats the supplied time using a cached formatter or in the absence of such a formatter with a newly created formatter
that will be cached.
|
String |
formatTime(Date time,
int style)
Formats the supplied time using the supplied style.
|
String |
formatTime(Date time,
String pattern)
Formats the supplied time using the supplied pattern.
|
NumberFormat |
getBigDecimalFormat()
Gets a formatter suitable for formatting BigDecimal numbers.
|
DateFormat |
getDateFormat()
Gets a formatter suitable for formatting
Dates . |
DateFormat |
getDateTimeFormat()
Gets a formatter suitable for formatting date/times.
|
NumberFormat |
getDecimalFormat()
Gets a formatter suitable for formatting decimal numbers.
|
DecimalFormat |
getIntegerFormat()
Gets a formatter suitable for formatting integer numbers.
|
static String |
getResourceBundleName()
Gets the name of the resource bundle that contains the localized descriptions for the messages.
|
DateFormat |
getShortDateFormat()
Gets a formatter suitable for formatting short
Dates . |
DateFormat |
getShortDateTimeFormat()
Gets a formatter suitable for formatting short date/times.
|
DateFormat |
getTimeFormat()
Gets a formatter suitable for formatting times.
|
TimeZone |
getTimeZone()
Gets the time zone.
|
boolean |
hasFourDigitYear()
Determines whether a parsed date, needs to have a year consisting of four digits.
|
BigDecimal |
parseBigDecimal(String bigDecimalNumber)
Parses a big decimal number that may not contain an exponent.
|
BigDecimal |
parseBigDecimal(String bigDecimalNumber,
String pattern)
Parses a big decimal number using the supplied pattern.
|
BigDecimal |
parseBigDecimalAcceptingAnExponent(String bigDecimalNumber)
Parses a big decimal number that may include an exponent.
|
Date |
parseDate(String date)
Parses a date.
|
Date |
parseDate(String date,
String pattern)
Parses a date using the supplied pattern.
|
Date |
parseDateTime(String dateTime)
Parses a date/time.
|
Date |
parseDateTime(String dateTime,
String pattern)
Parses a date/time using the supplied pattern.
|
Double |
parseDecimal(String decimalNumber)
Parses a decimal number that may not contain an exponent.
|
Double |
parseDecimal(String decimalNumber,
String pattern)
Parses a decimal number using the supplied pattern.
|
Double |
parseDecimalAcceptingAnExponent(String decimalNumber)
Parses a decimal number that may include an exponent.
|
Integer |
parseInteger(String integerNumber)
Parses an integer number.
|
Long |
parseLong(String longNumber)
Parses a long number.
|
Date |
parseTime(String time)
Parses a time.
|
static String |
removeSeparatorsFromDatePattern(String datePattern)
Removes all the separators from the supplied datePattern.
|
void |
setFourDigitYear(boolean fourDigitYear)
Forces a parsed date to have a year consisting of four digits or not.
|
void |
setTimeZone(String timeZone)
Sets the time zone.
|
void |
setTimeZone(TimeZone timeZone)
Sets the time zone.
|
static void |
validatePositionsOfGroupingSeparators(String decimalNumber,
NumberFormat numberFormat)
Validates that if the supplied decimalNumber contains grouping separators, they appear at the proper positions.
|
public static final String KEY_BOOLEAN_TRUE
public static final String KEY_BOOLEAN_FALSE
public static final String MSG_DATE_INVALID
public static final String MSG_DATE_INVALID_WITH_PATTERN
public static final String MSG_DATE_TIME_INVALID
public static final String MSG_DATE_TIME_INVALID_WITH_PATTERN
public static final String MSG_DATE_INVALID_YEAR
public static final String MSG_DECIMAL_INVALID
public static final String MSG_DECIMAL_INVALID_GROUPING
public static final String MSG_INTEGER_INVALID
public static final String MSG_INTEGER_OUT_OF_RANGE
public static final String MSG_LONG_INVALID
public static final String MSG_LONG_OUT_OF_RANGE
public static final String MSG_TIME_INVALID
public static final String MSG_TIME_INVALID_WITH_PATTERN
public Formatter(Locale locale)
locale
- Locale to be used for this formatter.NullPointerException
- When the supplied locale is null.public boolean hasFourDigitYear()
public void setFourDigitYear(boolean fourDigitYear)
fourDigitYear
- True for a four digit year, false to accept years of other lengths.public String formatBoolean(Boolean value)
value
- Boolean value to format.public String formatDate(Date date)
date
- Date to format.getDateFormat()
public String formatDate(Date date, int style)
date
- Date to format.style
- Style to apply to the output.
SimpleDateFormat
.IllegalArgumentException
- When no valid date style is supplied.public String formatDate(Date date, String pattern)
date
- Date to format.pattern
- Date pattern.
SimpleDateFormat
.IllegalArgumentException
- When the supplied pattern is empty or contains illegal data (does not form a valid
pattern).NullPointerException
- When the supplied pattern is null.public DateFormat getDateFormat()
Dates
.
Dates
.FormatterFactory.createDefaultDateFormat(Locale)
public DateFormat getShortDateFormat()
Dates
.
dates
.FormatterFactory.createDefaultShortDateFormat(Locale)
public Date parseDate(String date) throws InvalidArgumentException
setFourDigitYear(boolean)
.date
- Date to parse.Date
since this type is most often used in database applications.InvalidArgumentException
- When the supplied date does not represent a valid date.public Date parseDate(String date, String pattern) throws InvalidArgumentException
setFourDigitYear(boolean)
.date
- The date to parse.pattern
- The date pattern to use for parsing the supplied date.
Date
since this type is most often used in database applications.InvalidArgumentException
- When the supplied date does not represent a valid date or when the supplied pattern is
empty.public String formatDateTime(Date dateTime)
dateTime
- Date containing both date and time information to format.getDateTimeFormat()
public String formatDateTime(Date dateTime, int dateStyle, int timeStyle)
dateTime
- Date containing both date and time information to format.dateStyle
- Date style to apply to the output.
SimpleDateFormat
.timeStyle
- Time style to apply to the output.
SimpleDateFormat
.IllegalArgumentException
- When no valid date/time style is supplied.public String formatDateTime(Date dateTime, String pattern)
dateTime
- Date containing both date and time information to format.pattern
- Date/Time pattern.
SimpleDateFormat
.IllegalArgumentException
- When the supplied pattern is empty or contains illegal data (does not form a valid
pattern).NullPointerException
- When the supplied pattern is null.public DateFormat getDateTimeFormat()
FormatterFactory.createDefaultDateTimeFormat(Locale, TimeZone)
public DateFormat getShortDateTimeFormat()
FormatterFactory.createDefaultShortDateTimeFormat(Locale, TimeZone)
public Date parseDateTime(String dateTime) throws InvalidArgumentException
setFourDigitYear(boolean)
.dateTime
- Date containing date/time information to parse.Date
since this type is most often used in database applications.InvalidArgumentException
- When the supplied dateTime does not represent a valid date/time.public Date parseDateTime(String dateTime, String pattern) throws InvalidArgumentException
setFourDigitYear(boolean)
.dateTime
- Date containing date/time information to parse.pattern
- The date/time pattern to use for parsing the supplied dateTime.
Date
since this type is most often used in database applications.InvalidArgumentException
- When the supplied dateTime does not represent a valid date/time or when the supplied
pattern is empty.public String formatDecimal(Number number)
number
- Number to format.getDecimalFormat()
public String formatDecimal(Number number, String pattern)
RoundingMode.HALF_UP
rounding.number
- Number to format.pattern
- Number pattern.
DecimalFormat
.IllegalArgumentException
- When the supplied pattern is empty or contains illegal data (does not form a valid
pattern).NullPointerException
- When the supplied pattern is null.public String formatDecimal(Number number, int decimals)
RoundingMode.HALF_UP
rounding.number
- Number to format.decimals
- Number of decimals to show in the formatted result.public NumberFormat getDecimalFormat()
RoundingMode.HALF_UP
rounding.FormatterFactory.createDefaultDecimalFormat(Locale)
public Double parseDecimal(String decimalNumber) throws InvalidArgumentException
decimalNumber
- The decimal number to parse.InvalidArgumentException
- When the supplied decimalNumber does not represent a valid decimal number.
parseDecimalAcceptingAnExponent(String)
public Double parseDecimal(String decimalNumber, String pattern) throws InvalidArgumentException
decimalNumber
- The decimal number to parse.pattern
- The pattern to parse the supplied decimalNumber.
InvalidArgumentException
- When the supplied decimalNumber does not represent a valid decimal number.public Double parseDecimalAcceptingAnExponent(String decimalNumber) throws InvalidArgumentException
decimalNumber
- The decimal number to parse.InvalidArgumentException
- When the supplied decimalNumber does not represent a valid decimal number.public NumberFormat getBigDecimalFormat()
RoundingMode.HALF_UP
rounding.FormatterFactory.createDefaultBigDecimalFormat(Locale)
public BigDecimal parseBigDecimal(String bigDecimalNumber) throws InvalidArgumentException
bigDecimalNumber
- The big decimal number to parse.InvalidArgumentException
- When the supplied bigDecimalNumber does not represent a valid decimal number.
parseBigDecimalAcceptingAnExponent(String)
public BigDecimal parseBigDecimal(String bigDecimalNumber, String pattern) throws InvalidArgumentException
bigDecimalNumber
- The big decimal number to parse.pattern
- The pattern to parse the supplied bigDecimalNumber.
InvalidArgumentException
- When the supplied bigDecimalNumber does not represent a valid big decimal number.public BigDecimal parseBigDecimalAcceptingAnExponent(String bigDecimalNumber) throws InvalidArgumentException
bigDecimalNumber
- The big decimal number to parse.InvalidArgumentException
- When the supplied decimalNumber does not represent a valid decimal number.public static void validatePositionsOfGroupingSeparators(String decimalNumber, NumberFormat numberFormat) throws InvalidArgumentException
decimalNumber
- The decimal number to validate.
numberFormat
- The NumberFormat object used to parse the supplied decimalNumber.
InvalidArgumentException
- When the supplied decimalNumber contains grouping characters at incorrect positions.public String formatInteger(Number number)
number
- Number to format.getIntegerFormat()
public String formatInteger(Number number, String pattern)
number
- Number to format.pattern
- Number pattern.
DecimalFormat
.IllegalArgumentException
- When the supplied pattern is empty or contains illegal data,
(does not form a valid pattern).NullPointerException
- When the supplied pattern is null.public DecimalFormat getIntegerFormat()
FormatterFactory.createDefaultIntegerFormat(Locale)
public Integer parseInteger(String integerNumber) throws InvalidArgumentException
integerNumber
- The integer number to parse.InvalidArgumentException
- When the supplied integerNumber does not represent a valid integer number.public Long parseLong(String longNumber) throws InvalidArgumentException
longNumber
- The long number to parse.InvalidArgumentException
- When the supplied longNumber does not represent a valid long number.public String formatTime(Date time)
time
- Date containing time information to format.getTimeFormat()
public String formatTime(Date time, int style)
time
- Date containing time information to format.style
- Style to apply to the output.
SimpleDateFormat
.IllegalArgumentException
- When no valid time style is supplied.public String formatTime(Date time, String pattern)
time
- Date containing time information to format.pattern
- Time pattern.
SimpleDateFormat
.IllegalArgumentException
- When the supplied pattern is empty or contains illegal data
(does not form a valid pattern).NullPointerException
- When the supplied pattern is null.public DateFormat getTimeFormat()
FormatterFactory.createDefaultTimeFormat(Locale)
public Date parseTime(String time) throws InvalidArgumentException
time
- Date containing time information to parse.Date
since this type is most often used in database applications.InvalidArgumentException
- When the supplied time does not represent a valid time.public static String format(Object input)
input
- Input object to format.public static String format(Object input, String defaultValue)
input
- Input object to format.defaultValue
- Default value to use when the supplied input object is null.public static String format(Locale locale, String messagePattern, Object... messageArguments)
locale
- The locale to format the message.
messagePattern
- The message pattern.
messageArguments
- The arguments that must be inserted into the supplied messagePattern.
Localizable
that will be localized before being inserted into
the supplied messagePattern.IllegalArgumentException
- When the supplied messagePattern is empty or is not valid for the supplied
messagePattern.NullPointerException
- When the supplied messagePattern is null.public static String removeSeparatorsFromDatePattern(String datePattern)
datePattern
- The date pattern to remove separators from.
SimpleDateFormat
for examples of valid patterns.NullPointerException
- When the supplied datePattern is null.public static String getResourceBundleName()
public TimeZone getTimeZone()
public void setTimeZone(String timeZone)
timeZone
- The time zone to set.
TimeZone.getTimeZone(String)
public void setTimeZone(TimeZone timeZone)
timeZone
- The time zone to set.
Copyright © 2008–2018. All rights reserved.