DataFormatter

public final class DataFormatter

DataFormatter is a convenient class for formatting objects into or from string. Objects like Date, Time, and Decimal will be formatted according to the following default patterns if none is specified or configured beforehand:

  • date-format: yyyyMMdd
  • time-format: HHmmss
  • datetime-format: yyyyMMddHHmmss
  • timestamp-format: yyyyMMddHHmmssSSS
  • timestamp-suffix: 000
  • decimal-format: 0.00
Author:Hugo Y. K. Lam

Constructors

DataFormatter

public DataFormatter(Properties config)

Creates a new instance of DataFormatter.

Parameters:
  • config – configuration properties of the data format.

Methods

formatDate

public String formatDate(java.util.Date d)

Formats a java.util.Date value into a date String.

Parameters:
  • d – The value to be formatted.
Returns:

The String represenation of the value.

formatDate

public String formatDate(java.util.Date date, String pattern)

Formats a given java.util.Date into a String according to the specified pattern.

Parameters:
  • date – The date to be formatted.
  • pattern – The pattern to be followed in formatting.
Returns:

The formatted String of the date.

formatDate

public String formatDate(java.util.Date date, String pattern, Locale locale)

Formats a given java.util.Date into a String according to the specified pattern.

Parameters:
  • locale – The locale used in formatting.
  • date – The date to be formatted.
  • pattern – The pattern to be followed in formatting.
Returns:

The formatted String of the date.

formatDateTime

public String formatDateTime(java.util.Date d)

Formats a java.util.Date value into a date-time String.

Parameters:
  • d – The value to be formatted.
Returns:

The String represenation of the value.

formatDecimal

public String formatDecimal(double d)

Formats a double value into a String.

Parameters:
  • d – The value to be formatted.
Returns:

The String represenation of the value.

formatDecimal

public String formatDecimal(double d, String pattern)

Formats a given double into a String according to the specified pattern.

Parameters:
  • d – The double to be formatted.
  • pattern – The pattern to be followed in formatting.
Returns:

The formatted String of the double.

formatDecimal

public String formatDecimal(Double d)

Formats a Double value into a String.

Parameters:
  • d – The value to be formatted.
Returns:

The String represenation of the value.

formatTime

public String formatTime(java.util.Date d)

Formats a java.util.Date value into a time String.

Parameters:
  • d – The value to be formatted.
Returns:

The String represenation of the value.

formatTimestamp

public String formatTimestamp(java.util.Date d)

Formats a java.util.Date value into a timestamp String.

Parameters:
  • d – The value to be formatted.
Returns:

The String represenation of the value.

getInstance

public static DataFormatter getInstance()

Gets a default instance of DataFormatter.

Returns:a default instance of DataFormatter.

parseDate

public java.util.Date parseDate(String s)

Parses a date String and formats it into a java.util.Date object.

Parameters:
  • s – The value to be parsed.
Returns:

The Object represenation of the value.

parseDate

public java.util.Date parseDate(String date, String pattern)

Parses a date string and returns a java.util.Date object.

Parameters:
  • date – The date string to be parsed.
  • pattern – The pattern of the date string.
Returns:

A java.util.Date object that represents the given date string.

parseDate

public java.util.Date parseDate(String date, String pattern, Locale locale)

Parses a date string and returns a java.util.Date object.

Parameters:
  • date – The date string to be parsed.
  • pattern – The pattern of the date string.
  • locale – The locale used in parsing the date string.
Returns:

A java.util.Date object that represents the given date string.

parseDateTime

public java.util.Date parseDateTime(String s)

Parses a date-time String and formats it into a java.util.Date object.

Parameters:
  • s – The value to be parsed.
Returns:

The Object represenation of the value.

parseTime

public java.util.Date parseTime(String s)

Parses a time String and formats it into a java.util.Date object.

Parameters:
  • s – The value to be parsed.
Returns:

The Object represenation of the value.

parseTimestamp

public java.util.Date parseTimestamp(String s)

Parses a timestamp String and formats it into a java.util.Date object.

Parameters:
  • s – The value to be parsed.
Returns:

The Object represenation of the value.