Class FormatUtils
- java.lang.Object
-
- com.microstrategy.utils.FormatUtils
-
public class FormatUtils extends java.lang.Object
This class provides support for parsing and formatting different data types used in our application. Parsing and formatting for different locales is configurable via the configuration XML file. This is an utility class containing only static methods.There are pairs of parse/format methods for each supported data type. The parse methods take the string and locale parameters and return proper data type. The format methods take a number or date object and locale and return formatted string. In all cases if configuration XML does not contain formatting information for requested locale and data type proper default format will be used.
The parseInteger and parseNumber methods shall be used for parsing numbers. The former accepts only integers while the later accepts both integers and fractional numbers.
There are several methods for parsing date and time data. The parseAnyDate method accepts strings representing date, time or date-time. It first uses date-time patterns, then date patterns and then time patterns. The parseDate, parseTime and parseDateTime methods accept only one kind of input.
The normalizeNumber and normalizeAnyDate methods can be used to produce "normalized" representation of input string. This representation is based on corresponding output pattern. Those methods recognize the kind of input string and produce corresponding output. For example if input string contains date without time the output string will also contain date without time.
The converNumber and convertAnyDate methods can be used to convert numbers or dates from one locale to another. There are two versions of each of this method. One that takes original string from-locale and to-locale and another that takes only original string and to-locale. The last one always converts into the standard (US) locale. All those methods recognize the kind of input string and produce corresponding output. For example if input string contains date without time the output string will also contain date without time.
The convertNumberPattern and convertDatePattern methods convert corresponding patterns from a specific locale into another one. There are two versions of each of this method. One that takes original pattern from-locale and to-locale and another that takes only original pattern and to-locale. The last one always converts into the standard (US) locale.
From 8.0, we introduce a child node "display" into each locale setting. The child text node of this node will define the desired format for diplaying different types of value in web page. When the "display" node is missing (either whole locale is missing, or just "display" node is missing), the output format define for the same locale will be used, except for date/time object. The default format for date and time is SHORT style for date and MEDIUM style for time. A set of display method will be used to formatting object according to the format defination in these "display" nodes.
- Since:
- MicroStrategy Web 7.3.1 or earlier
-
-
Constructor Summary
Constructors Constructor Description FormatUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
convertAnyDate(java.lang.String str, java.util.Locale fromLoc)
Converts input string from specified locale into default US locale.static java.lang.String
convertAnyDate(java.lang.String str, java.util.Locale fromLoc, java.util.Locale toLoc)
Converts input string from fromLoc locale into toLoc locale.static java.lang.String
convertBigDecimal(java.lang.String str, java.util.Locale fromLoc, java.util.Locale toLoc)
Converts a big decimal string fromfromLoc
intotoLoc
.static java.lang.String
convertNumber(java.lang.String str, java.util.Locale fromLoc)
Converts input string from specified locale into default US locale.static java.lang.String
convertNumber(java.lang.String str, java.util.Locale fromLoc, java.util.Locale toLoc)
Converts input string from fromLoc locale into toLoc locale.static java.lang.String
convertNumberPattern(java.lang.String pattern, java.util.Locale fromLoc)
Converts number pattern from specified locale into default US locale.static java.lang.String
convertNumberPattern(java.lang.String pattern, java.util.Locale fromLoc, java.util.Locale toLoc)
Converts number pattern from fromLoc into toLoc.static void
destroy()
Destroy the HashSet and other caches, will be called when destroying the sevletstatic java.lang.String
displayDate(java.util.Date date, java.util.Locale loc)
Display a Date object using date DISPLAY format for specified localestatic java.lang.String
displayDateTime(java.util.Date date, java.util.Locale loc)
Displays a Date object using date-time DISPLAY format for specified localestatic java.lang.String
displayInteger(long number, java.util.Locale loc)
Formats a number using interger DISPLAY format for specified localestatic java.lang.String
displayNumber(double number, java.util.Locale loc)
Displays a number using number DISPLAY format for specified localestatic java.lang.String
displayTime(java.util.Date date, java.util.Locale loc)
Displays a Date object using time DISPLAY format for specified localestatic java.lang.String
formatDate(java.util.Date date, java.util.Locale loc)
Formats a Date object using date output format for specified localestatic java.lang.String
formatDateTime(java.util.Date date, java.util.Locale loc)
Formats a Date object using date-time output format for specified localestatic java.lang.String
formatInteger(long number, java.util.Locale loc)
Formats a number using integer output format for specified localestatic java.lang.String
formatNumber(double number, java.util.Locale loc)
Formats a number using number output format for specified localestatic java.lang.String
formatNumber(java.lang.Number number, java.util.Locale loc)
Formats a number using number output format for specified localestatic java.lang.String
formatTime(java.util.Date date, java.util.Locale loc)
Formats a Date object using time output format for specified localestatic java.lang.String
getDateDisplayFormatPattern(java.util.Locale loc)
static java.util.List<java.lang.String>
getDateInputFormatPatterns(java.util.Locale loc)
static java.text.DateFormat
getDateOutputFormat(java.util.Locale loc)
ReturnsDateFormat
for specified locale.static java.lang.String
getDateOutputFormatPattern(java.util.Locale loc)
static java.text.DateFormat
getDateTimeDisplayFormat(java.util.Locale loc)
static java.lang.String
getFormatXML(java.util.Locale loc)
static java.util.List<java.lang.String>
getIntegerInputFormatPatterns(java.util.Locale loc)
static java.lang.String
getIntegerOutputFormatPattern(java.util.Locale loc)
static java.util.List<java.lang.String>
getNumberInputFormatPatterns(java.util.Locale loc)
static java.text.DecimalFormat
getNumberOutputFormat(java.util.Locale loc)
static java.lang.String
getNumberOutputFormatPattern(java.util.Locale loc)
static java.lang.String
getSeparatorFormat(java.util.Locale loc)
static java.util.Set
getSupportedLocales()
Returns a set containing supported locales.static java.lang.String
getTimeDisplayFormatPattern(java.util.Locale loc)
static java.util.List<java.lang.String>
getTimeInputFormatPatterns(java.util.Locale loc)
static java.text.DateFormat
getTimeOutputFormat(java.util.Locale loc)
static java.lang.String
getTimeOutputFormatPattern(java.util.Locale loc)
static void
init(java.lang.String filePath)
Re-initializes the class using configuration XML file.static java.lang.String
normalizeAnyDate(java.lang.String str, java.util.Locale loc)
Converts input string into standard format.static java.lang.String
normalizeNumber(java.lang.String str, java.util.Locale loc)
Converts input string into standard format.static java.util.Date
parseAnyDate(java.lang.String str, java.util.Locale loc)
Parses input string using all available date patterns for specified locale.static java.util.Date
parseDate(java.lang.String str, java.util.Locale loc)
Parses input string using date patterns for specified locale.static java.util.Date
parseDateTime(java.lang.String str, java.util.Locale loc)
Parses input string using date-time patterns for specified locale.static java.lang.String
parseInputForIServer(java.lang.String input, int dataType, java.util.Locale loc)
Return a string representation of user input that can be appropriately handled by IServerstatic java.lang.Number
parseInteger(java.lang.String str, java.util.Locale loc)
Parses input string using integer patterns for specified locale.static java.lang.Number
parseNumber(java.lang.String str, java.util.Locale loc)
Parses input string using number patterns for specified locale.static java.util.Date
parseTime(java.lang.String str, java.util.Locale loc)
Parses input string using time patterns for specified locale.static boolean
validateBigDecimal(java.lang.String value, java.util.Locale locale)
Returns whetherparseNumber(String, Locale)
succeeded or not.
-
-
-
Method Detail
-
init
public static void init(java.lang.String filePath) throws WebUtilsException
Re-initializes the class using configuration XML file. (Original initialization is done in the static constructor).- Parameters:
filePath
- input stream of configuration XML- Throws:
WebUtilsException
-
destroy
public static void destroy()
Destroy the HashSet and other caches, will be called when destroying the sevlet
-
parseInteger
public static java.lang.Number parseInteger(java.lang.String str, java.util.Locale loc) throws java.text.ParseException
Parses input string using integer patterns for specified locale. This method accepts only integer numbers.- Parameters:
str
- input stringloc
- locale- Returns:
- parsed number
- Throws:
java.text.ParseException
- if no one pattern accepted input string
-
formatInteger
public static java.lang.String formatInteger(long number, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a number using integer output format for specified locale- Parameters:
number
- a number to be formattedloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
displayInteger
public static java.lang.String displayInteger(long number, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a number using interger DISPLAY format for specified locale- Parameters:
number
- long a number to be displayed in web pageloc
- Locale- Returns:
- String formatted number string
- Throws:
java.lang.IllegalArgumentException
- Since:
- MicroStrategy Web 7.5.4
-
parseNumber
public static java.lang.Number parseNumber(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Parses input string using number patterns for specified locale. This method accepts both decimal and fractional numbers.- Parameters:
str
- input stringloc
- locale- Returns:
- parsed number
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
formatNumber
public static java.lang.String formatNumber(double number, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a number using number output format for specified locale- Parameters:
number
- a number to be formattedloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
-
displayNumber
public static java.lang.String displayNumber(double number, java.util.Locale loc) throws java.lang.IllegalArgumentException
Displays a number using number DISPLAY format for specified locale- Parameters:
number
- a number to be displayed in web pageloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
- Since:
- MicroStrategy Web 7.5.4
-
formatNumber
public static java.lang.String formatNumber(java.lang.Number number, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a number using number output format for specified locale- Parameters:
number
- a Number object to be formattedloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
-
getNumberOutputFormat
public static java.text.DecimalFormat getNumberOutputFormat(java.util.Locale loc)
- Since:
- MicroStrategy Web 8.1.2
-
parseAnyDate
public static java.util.Date parseAnyDate(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Parses input string using all available date patterns for specified locale. This method accepts any date string. First it tries parsing input string using date-time patterns, then using date patterns and then using time patterns. This process stops at first successfull try.- Parameters:
str
- input stringloc
- locale- Returns:
- parsed number
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
parseDate
public static java.util.Date parseDate(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Parses input string using date patterns for specified locale. This method accepts dates without time.- Parameters:
str
- input stringloc
- locale- Returns:
- parsed date
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
formatDate
public static java.lang.String formatDate(java.util.Date date, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a Date object using date output format for specified locale- Parameters:
date
- a date to be formattedloc
- locale- Returns:
- formatted date string
- Throws:
java.lang.IllegalArgumentException
-
displayDate
public static java.lang.String displayDate(java.util.Date date, java.util.Locale loc) throws java.lang.IllegalArgumentException
Display a Date object using date DISPLAY format for specified locale- Parameters:
date
- a date to be displayedloc
- locale- Returns:
- formatted date string
- Throws:
java.lang.IllegalArgumentException
- Since:
- MicroStrategy Web 7.5.4
-
getDateOutputFormat
public static java.text.DateFormat getDateOutputFormat(java.util.Locale loc)
ReturnsDateFormat
for specified locale.- Parameters:
loc
- locale- Returns:
DateFormat
for the specified locale.- Since:
- MicroStrategy Web 7.5.1
-
getDateInputFormatPatterns
public static java.util.List<java.lang.String> getDateInputFormatPatterns(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getTimeInputFormatPatterns
public static java.util.List<java.lang.String> getTimeInputFormatPatterns(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getIntegerInputFormatPatterns
public static java.util.List<java.lang.String> getIntegerInputFormatPatterns(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getNumberInputFormatPatterns
public static java.util.List<java.lang.String> getNumberInputFormatPatterns(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getDateOutputFormatPattern
public static java.lang.String getDateOutputFormatPattern(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getDateDisplayFormatPattern
public static java.lang.String getDateDisplayFormatPattern(java.util.Locale loc)
-
getTimeOutputFormatPattern
public static java.lang.String getTimeOutputFormatPattern(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getTimeDisplayFormatPattern
public static java.lang.String getTimeDisplayFormatPattern(java.util.Locale loc)
-
getIntegerOutputFormatPattern
public static java.lang.String getIntegerOutputFormatPattern(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getNumberOutputFormatPattern
public static java.lang.String getNumberOutputFormatPattern(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
getTimeOutputFormat
public static java.text.DateFormat getTimeOutputFormat(java.util.Locale loc)
- Since:
- MicroStrategy Web 8.1.2
-
getDateTimeDisplayFormat
public static java.text.DateFormat getDateTimeDisplayFormat(java.util.Locale loc)
- Since:
- MicroStrategy Web 7.5.4
-
getSeparatorFormat
public static java.lang.String getSeparatorFormat(java.util.Locale loc)
- Since:
- MicroStrategy Web 9.0.0
-
parseTime
public static java.util.Date parseTime(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Parses input string using time patterns for specified locale. This method accepts strings containing time only.- Parameters:
str
- input stringloc
- locale- Returns:
- parsed number
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
formatTime
public static java.lang.String formatTime(java.util.Date date, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a Date object using time output format for specified locale- Parameters:
date
- a date to be formattedloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
-
displayTime
public static java.lang.String displayTime(java.util.Date date, java.util.Locale loc) throws java.lang.IllegalArgumentException
Displays a Date object using time DISPLAY format for specified locale- Parameters:
date
- a date to be displayed in web pageloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
- Since:
- MicroStrategy Web 7.5.4
-
parseDateTime
public static java.util.Date parseDateTime(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Parses input string using date-time patterns for specified locale. This method accepts only date-time strings.- Parameters:
str
- input stringloc
- locale- Returns:
- parsed date
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
formatDateTime
public static java.lang.String formatDateTime(java.util.Date date, java.util.Locale loc) throws java.lang.IllegalArgumentException
Formats a Date object using date-time output format for specified locale- Parameters:
date
- a date to be formattedloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
-
displayDateTime
public static java.lang.String displayDateTime(java.util.Date date, java.util.Locale loc) throws java.lang.IllegalArgumentException
Displays a Date object using date-time DISPLAY format for specified locale- Parameters:
date
- a date to be displayed in web pageloc
- locale- Returns:
- formatted number string
- Throws:
java.lang.IllegalArgumentException
- Since:
- MicroStrategy Web 7.5.4
-
normalizeNumber
public static java.lang.String normalizeNumber(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Converts input string into standard format. First this method parses input string then formats it using proper output format. This method recognizes the kind of input string, if it represents integer then integer output format will be used, otherwise the number output format will be used.- Parameters:
str
- input stringloc
- locale- Returns:
- normalized string
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
normalizeAnyDate
public static java.lang.String normalizeAnyDate(java.lang.String str, java.util.Locale loc) throws java.text.ParseException, java.lang.IllegalArgumentException
Converts input string into standard format. First this method parses input string then formats it using proper output format. This method recognizes the kind of input string. For example if input string contains date without time then the date output format will be used.- Parameters:
str
- input stringloc
- locale- Returns:
- normalized string
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
convertNumber
public static java.lang.String convertNumber(java.lang.String str, java.util.Locale fromLoc) throws java.text.ParseException, java.lang.IllegalArgumentException
Converts input string from specified locale into default US locale. First this method parses input string using fromLoc patterns then formats it using proper US locale output format. This method recognizes the kind of input string, if it represents integer then integer output format will be used, otherwise the number output format will be used.- Parameters:
str
- input stringfromLoc
- from locale- Returns:
- converted string
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterloc
is null.
-
convertNumber
public static java.lang.String convertNumber(java.lang.String str, java.util.Locale fromLoc, java.util.Locale toLoc) throws java.text.ParseException, java.lang.IllegalArgumentException
Converts input string from fromLoc locale into toLoc locale. First this method parses input string using fromLoc patterns then formats it using proper toLoc output format. This method recognizes the kind of input string, if it represents integer then integer output format will be used, otherwise the number output format will be used.- Parameters:
str
- input stringfromLoc
- from localetoLoc
- to locale- Returns:
- converted string
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- Thrown when input parameterfromLoc
ortoLoc
is null.
-
convertAnyDate
public static java.lang.String convertAnyDate(java.lang.String str, java.util.Locale fromLoc) throws java.text.ParseException, java.lang.IllegalArgumentException
Converts input string from specified locale into default US locale. First this method parses input string using fromLoc patterns then formats it using proper US locale output format. This method recognizes the kind of input string. For example if input string contains date without time then the date output format will be used.- Parameters:
str
- input stringfromLoc
- from locale- Returns:
- converted string
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- if the input locale is null
-
convertAnyDate
public static java.lang.String convertAnyDate(java.lang.String str, java.util.Locale fromLoc, java.util.Locale toLoc) throws java.text.ParseException, java.lang.IllegalArgumentException
Converts input string from fromLoc locale into toLoc locale. First this method parses input string using fromLoc patterns then formats it using proper toLoc output format. This method recognizes the kind of input string. For example if input string contains date without time then the date output format will be used.- Parameters:
str
- input stringfromLoc
- from localetoLoc
- to locale- Returns:
- converted string
- Throws:
java.text.ParseException
- if no one pattern accepted input stringjava.lang.IllegalArgumentException
- if the input locale is null
-
convertNumberPattern
public static java.lang.String convertNumberPattern(java.lang.String pattern, java.util.Locale fromLoc) throws java.lang.IllegalArgumentException
Converts number pattern from specified locale into default US locale. Conversion is done by replacing pattern symbols specific to fromLoc locale with corresponding symbols for US locale.- Parameters:
pattern
- input stringfromLoc
- from locale- Returns:
- converted string
- Throws:
java.lang.IllegalArgumentException
- if the input locale is null
-
convertNumberPattern
public static java.lang.String convertNumberPattern(java.lang.String pattern, java.util.Locale fromLoc, java.util.Locale toLoc) throws java.lang.IllegalArgumentException
Converts number pattern from fromLoc into toLoc. Conversion is done by replacing pattern symbols specific to fromLoc locale with corresponding symbols for toLoc locale.- Parameters:
pattern
- input stringfromLoc
- from localetoLoc
- to locale- Returns:
- converted string
- Throws:
java.lang.IllegalArgumentException
- if the input locale is null
-
getSupportedLocales
public static java.util.Set getSupportedLocales()
Returns a set containing supported locales.- Returns:
- set containing supported locales
-
validateBigDecimal
public static boolean validateBigDecimal(java.lang.String value, java.util.Locale locale)
Returns whetherparseNumber(String, Locale)
succeeded or not.- Parameters:
value
- input stringlocale
- locale- Returns:
- whether
parseNumber(String, Locale)
succeeded or not - Since:
- MicroStrategy Web 8.0.0
-
convertBigDecimal
public static java.lang.String convertBigDecimal(java.lang.String str, java.util.Locale fromLoc, java.util.Locale toLoc) throws java.lang.IllegalArgumentException
Converts a big decimal string fromfromLoc
intotoLoc
. Conversion is done by replacing pattern symbols specific to fromLoc locale with corresponding symbols for toLoc locale.- Parameters:
str
- big decimal patternfromLoc
- source localetoLoc
- target locale- Returns:
- The converted big decimal string from
fromLoc
intotoLoc
. - Throws:
java.lang.IllegalArgumentException
- if the input locale is null- Since:
- MicroStrategy Web 8.0.0
-
getFormatXML
public static java.lang.String getFormatXML(java.util.Locale loc)
-
parseInputForIServer
public static java.lang.String parseInputForIServer(java.lang.String input, int dataType, java.util.Locale loc) throws java.text.ParseException
Return a string representation of user input that can be appropriately handled by IServer- Parameters:
input
-String
inputdataType
- int fromEnumDSSXMLDataType
loc
- current session's numberLocale
- Returns:
String
containing value of input- Throws:
java.text.ParseException
-
-