Class FormatHelper

java.lang.Object
org.tentackle.misc.FormatHelper

public class FormatHelper extends Object
Formatting helper methods.

The localized patterns are taken from the MiscCoreBundle and can be replaced per locale via applyPatterns(Locale, Patterns). Methods without a locale parameter refer to the current locale of the LocaleProvider.

The parseXXX methods parse the exact format. For the smart parsers accepting shortcuts and incomplete input, see DateTimeUtilities.

Author:
harald
  • Method Details

    • applyPatterns

      public static void applyPatterns(Locale locale, FormatHelper.Patterns patterns)
      Applies a set of patterns to a locale.
      Replaces the patterns from the MiscCoreBundle.
      Parameters:
      locale - the locale
      patterns - the patterns
    • applyPatterns

      public static void applyPatterns(FormatHelper.Patterns patterns)
      Applies a set of patterns to the current locale.
      Parameters:
      patterns - the patterns
    • getIntegerPattern

      public static String getIntegerPattern(Locale locale)
      Gets the integer pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getFloatingNumberPattern

      public static String getFloatingNumberPattern(Locale locale)
      Gets the floating number pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getMoneyPattern

      public static String getMoneyPattern(Locale locale)
      Gets the money pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getTimestampPattern

      public static String getTimestampPattern(Locale locale)
      Gets the timestamp pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getTimestampPatternWithOffset

      public static String getTimestampPatternWithOffset(Locale locale)
      Gets the timestamp pattern with zone offset.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getTimestampPatternWithTimeZone

      public static String getTimestampPatternWithTimeZone(Locale locale)
      Gets the timestamp pattern with timezone.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getShortTimestampPattern

      public static String getShortTimestampPattern(Locale locale)
      Gets the short timestamp pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getShortTimestampPatternWithOffset

      public static String getShortTimestampPatternWithOffset(Locale locale)
      Gets the short timestamp pattern with zone offset.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getShortTimestampPatternWithTimeZone

      public static String getShortTimestampPatternWithTimeZone(Locale locale)
      Gets the short timestamp pattern with timezone.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getDatePattern

      public static String getDatePattern(Locale locale)
      Gets the date pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getShortDatePattern

      public static String getShortDatePattern(Locale locale)
      Gets the short date pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getTimePattern

      public static String getTimePattern(Locale locale)
      Gets the time pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getTimePatternWithOffset

      public static String getTimePatternWithOffset(Locale locale)
      Gets the time pattern with zone offset.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getShortTimePattern

      public static String getShortTimePattern(Locale locale)
      Gets the short time pattern.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getShortTimePatternWithOffset

      public static String getShortTimePatternWithOffset(Locale locale)
      Gets the short time pattern with zone offset.
      Parameters:
      locale - the locale
      Returns:
      the pattern
    • getIntegerPattern

      public static String getIntegerPattern()
      Gets the integer pattern.
      Returns:
      the pattern
    • getFloatingNumberPattern

      public static String getFloatingNumberPattern()
      Gets the floating number pattern.
      Returns:
      the pattern
    • getMoneyPattern

      public static String getMoneyPattern()
      Gets the money pattern.
      Returns:
      the pattern
    • getTimestampPattern

      public static String getTimestampPattern()
      Gets the timestamp pattern.
      Returns:
      the pattern
    • getTimestampPatternWithOffset

      public static String getTimestampPatternWithOffset()
      Gets the timestamp pattern with zone offset.
      Returns:
      the pattern
    • getTimestampPatternWithTimeZone

      public static String getTimestampPatternWithTimeZone()
      Gets the timestamp pattern with timezone.
      Returns:
      the pattern
    • getShortTimestampPattern

      public static String getShortTimestampPattern()
      Gets the short timestamp pattern.
      Returns:
      the pattern
    • getShortTimestampPatternWithOffset

      public static String getShortTimestampPatternWithOffset()
      Gets the short timestamp pattern with offset.
      Returns:
      the pattern
    • getShortTimestampPatternWithTimeZone

      public static String getShortTimestampPatternWithTimeZone()
      Gets the short timestamp pattern with timezone.
      Returns:
      the pattern
    • getDatePattern

      public static String getDatePattern()
      Gets the date pattern.
      Returns:
      the pattern
    • getShortDatePattern

      public static String getShortDatePattern()
      Gets the short date pattern.
      Returns:
      the pattern
    • getTimePattern

      public static String getTimePattern()
      Gets the time pattern.
      Returns:
      the pattern
    • getTimePatternWithOffset

      public static String getTimePatternWithOffset()
      Gets the time pattern with zone offset.
      Returns:
      the pattern
    • getShortTimePattern

      public static String getShortTimePattern()
      Gets the short time pattern.
      Returns:
      the pattern
    • getShortTimePatternWithOffset

      public static String getShortTimePatternWithOffset()
      Gets the short time pattern with zone offset.
      Returns:
      the pattern
    • parseInteger

      public static int parseInteger(String str) throws ParseException
      Parses an integer.
      Parameters:
      str - the string to parse
      Returns:
      the number
      Throws:
      ParseException - if parsing failed
    • parseFloat

      public static float parseFloat(String str) throws ParseException
      Parses a float.
      Parameters:
      str - the string to parse
      Returns:
      the number
      Throws:
      ParseException - if parsing failed
    • parseDouble

      public static double parseDouble(String str) throws ParseException
      Parses a double.
      Parameters:
      str - the string to parse
      Returns:
      the number
      Throws:
      ParseException - if parsing failed
    • parseTimestamp

      public static Timestamp parseTimestamp(String str)
      Parses a timestamp.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortTimestamp

      public static Timestamp parseShortTimestamp(String str)
      Parses a short timestamp.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseDate

      public static Date parseDate(String str)
      Parses a date.
      Parameters:
      str - the string to parse
      Returns:
      the date
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortDate

      public static Date parseShortDate(String str)
      Parses a short date.
      Parameters:
      str - the string to parse
      Returns:
      the date
      Throws:
      DateTimeParseException - if parsing failed
    • parseTime

      public static Time parseTime(String str)
      Parses a time.
      Parameters:
      str - the string to parse
      Returns:
      the time
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortTime

      public static Time parseShortTime(String str)
      Parses a short time.
      Parameters:
      str - the string to parse
      Returns:
      the time
      Throws:
      DateTimeParseException - if parsing failed
    • parseLocalDateTime

      public static LocalDateTime parseLocalDateTime(String str)
      Parses a timestamp.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseOffsetDateTime

      public static OffsetDateTime parseOffsetDateTime(String str)
      Parses a timestamp with zone offset.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseZonedDateTime

      public static ZonedDateTime parseZonedDateTime(String str)
      Parses a timestamp with timezone.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortLocalDateTime

      public static LocalDateTime parseShortLocalDateTime(String str)
      Parses a short timestamp.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortOffsetDateTime

      public static OffsetDateTime parseShortOffsetDateTime(String str)
      Parses a short timestamp with zone offset.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortZonedDateTime

      public static ZonedDateTime parseShortZonedDateTime(String str)
      Parses a short timestamp with timezone.
      Parameters:
      str - the string to parse
      Returns:
      the timestamp
      Throws:
      DateTimeParseException - if parsing failed
    • parseLocalDate

      public static LocalDate parseLocalDate(String str)
      Parses a date.
      Parameters:
      str - the string to parse
      Returns:
      the date
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortLocalDate

      public static LocalDate parseShortLocalDate(String str)
      Parses a short date.
      Parameters:
      str - the string to parse
      Returns:
      the date
      Throws:
      DateTimeParseException - if parsing failed
    • parseLocalTime

      public static LocalTime parseLocalTime(String str)
      Parses a time.
      Parameters:
      str - the string to parse
      Returns:
      the time
      Throws:
      DateTimeParseException - if parsing failed
    • parseOffsetTime

      public static OffsetTime parseOffsetTime(String str)
      Parses a time with zone offset.
      Parameters:
      str - the string to parse
      Returns:
      the time
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortLocalTime

      public static LocalTime parseShortLocalTime(String str)
      Parses a short time.
      Parameters:
      str - the string to parse
      Returns:
      the time
      Throws:
      DateTimeParseException - if parsing failed
    • parseShortOffsetTime

      public static OffsetTime parseShortOffsetTime(String str)
      Parses short a time with zone offset.
      Parameters:
      str - the string to parse
      Returns:
      the time
      Throws:
      DateTimeParseException - if parsing failed
    • formatInteger

      public static String formatInteger(Number number)
      Formats an integer.
      Parameters:
      number - the number to format
      Returns:
      the formatted string
    • formatFloatingNumber

      public static String formatFloatingNumber(Number number)
      Formats a floating number.
      Parameters:
      number - the number to format
      Returns:
      the formatted string
    • formatTimestamp

      public static String formatTimestamp(Date timestamp)
      Formats a timestamp.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatShortTimestamp

      public static String formatShortTimestamp(Date timestamp)
      Formats a short timestamp.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatDate

      public static String formatDate(Date date)
      Formats a date.
      Parameters:
      date - the date to format
      Returns:
      the formatted string
    • formatShortDate

      public static String formatShortDate(Date date)
      Formats a short date.
      Parameters:
      date - the date to format
      Returns:
      the formatted string
    • formatTime

      public static String formatTime(Date time)
      Formats a time.
      Parameters:
      time - the time to format
      Returns:
      the formatted string
    • formatShortTime

      public static String formatShortTime(Date time)
      Formats a short time.
      Parameters:
      time - the time to format
      Returns:
      the formatted string
    • formatLocalDateTime

      public static String formatLocalDateTime(LocalDateTime timestamp)
      Formats a timestamp.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatOffsetDateTime

      public static String formatOffsetDateTime(OffsetDateTime timestamp)
      Formats a timestamp with zone offset.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatZonedDateTime

      public static String formatZonedDateTime(ZonedDateTime timestamp)
      Formats a timestamp with timezone.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatShortLocalDateTime

      public static String formatShortLocalDateTime(LocalDateTime timestamp)
      Formats a short timestamp.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatShortOffsetDateTime

      public static String formatShortOffsetDateTime(OffsetDateTime timestamp)
      Formats a short timestamp with zone offset.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatShortZonedDateTime

      public static String formatShortZonedDateTime(ZonedDateTime timestamp)
      Formats a short timestamp with timezone.
      Parameters:
      timestamp - the timestamp to format
      Returns:
      the formatted string
    • formatLocalDate

      public static String formatLocalDate(LocalDate date)
      Formats a date.
      Parameters:
      date - the date to format
      Returns:
      the formatted string
    • formatShortLocalDate

      public static String formatShortLocalDate(LocalDate date)
      Formats a short date.
      Parameters:
      date - the date to format
      Returns:
      the formatted string
    • formatLocalTime

      public static String formatLocalTime(LocalTime time)
      Formats a time.
      Parameters:
      time - the time to format
      Returns:
      the formatted string
    • formatOffsetTime

      public static String formatOffsetTime(OffsetTime time)
      Formats a time with zone offset.
      Parameters:
      time - the time to format
      Returns:
      the formatted string
    • formatShortLocalTime

      public static String formatShortLocalTime(LocalTime time)
      Formats a short time.
      Parameters:
      time - the time to format
      Returns:
      the formatted string
    • formatShortOffsetTime

      public static String formatShortOffsetTime(OffsetTime time)
      Formats a short time with zone offset.
      Parameters:
      time - the time to format
      Returns:
      the formatted string
    • getLocalDateTimeFormatter

      public static DateTimeFormatter getLocalDateTimeFormatter()
      Gets the formatter for a LocalDateTime.
      Returns:
      the immutable and thread-safe formatter
    • getOffsetDateTimeFormatter

      public static DateTimeFormatter getOffsetDateTimeFormatter()
      Gets the formatter for an OffsetDateTime.
      Returns:
      the immutable and thread-safe formatter
    • getZonedDateTimeFormatter

      public static DateTimeFormatter getZonedDateTimeFormatter()
      Gets the formatter for a ZonedDateTime.
      Returns:
      the immutable and thread-safe formatter
    • getLocalDateFormatter

      public static DateTimeFormatter getLocalDateFormatter()
      Gets the formatter for a LocalDate.
      Returns:
      the immutable and thread-safe formatter
    • getLocalTimeFormatter

      public static DateTimeFormatter getLocalTimeFormatter()
      Gets the formatter for a LocalTime.
      Returns:
      the immutable and thread-safe formatter
    • getOffsetTimeFormatter

      public static DateTimeFormatter getOffsetTimeFormatter()
      Gets the formatter for an OffsetTime.
      Returns:
      the immutable and thread-safe formatter
    • getShortLocalDateTimeFormatter

      public static DateTimeFormatter getShortLocalDateTimeFormatter()
      Gets the formatter for a short LocalDateTime.
      Returns:
      the immutable and thread-safe formatter
    • getShortOffsetDateTimeFormatter

      public static DateTimeFormatter getShortOffsetDateTimeFormatter()
      Gets the formatter for a short OffsetDateTime.
      Returns:
      the immutable and thread-safe formatter
    • getShortZonedDateTimeFormatter

      public static DateTimeFormatter getShortZonedDateTimeFormatter()
      Gets the formatter for a short ZonedDateTime.
      Returns:
      the immutable and thread-safe formatter
    • getShortLocalDateFormatter

      public static DateTimeFormatter getShortLocalDateFormatter()
      Gets the formatter for a short LocalDate.
      Returns:
      the immutable and thread-safe formatter
    • getShortLocalTimeFormatter

      public static DateTimeFormatter getShortLocalTimeFormatter()
      Gets the formatter for a short LocalTime.
      Returns:
      the immutable and thread-safe formatter
    • getShortOffsetTimeFormatter

      public static DateTimeFormatter getShortOffsetTimeFormatter()
      Gets the formatter for a short OffsetTime.
      Returns:
      the immutable and thread-safe formatter
    • getLocalDateTimeFormatter

      public static DateTimeFormatter getLocalDateTimeFormatter(Locale locale)
      Gets the formatter for a LocalDateTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getOffsetDateTimeFormatter

      public static DateTimeFormatter getOffsetDateTimeFormatter(Locale locale)
      Gets the formatter for an OffsetDateTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getZonedDateTimeFormatter

      public static DateTimeFormatter getZonedDateTimeFormatter(Locale locale)
      Gets the formatter for a ZonedDateTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getLocalDateFormatter

      public static DateTimeFormatter getLocalDateFormatter(Locale locale)
      Gets the formatter for a LocalDate.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getLocalTimeFormatter

      public static DateTimeFormatter getLocalTimeFormatter(Locale locale)
      Gets the formatter for a LocalTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getOffsetTimeFormatter

      public static DateTimeFormatter getOffsetTimeFormatter(Locale locale)
      Gets the formatter for an OffsetTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getShortLocalDateTimeFormatter

      public static DateTimeFormatter getShortLocalDateTimeFormatter(Locale locale)
      Gets the formatter for a short LocalDateTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getShortOffsetDateTimeFormatter

      public static DateTimeFormatter getShortOffsetDateTimeFormatter(Locale locale)
      Gets the formatter for a short OffsetDateTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getShortZonedDateTimeFormatter

      public static DateTimeFormatter getShortZonedDateTimeFormatter(Locale locale)
      Gets the formatter for a short ZonedDateTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getShortLocalDateFormatter

      public static DateTimeFormatter getShortLocalDateFormatter(Locale locale)
      Gets the formatter for a short LocalDate.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getShortLocalTimeFormatter

      public static DateTimeFormatter getShortLocalTimeFormatter(Locale locale)
      Gets the formatter for a short LocalTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • getShortOffsetTimeFormatter

      public static DateTimeFormatter getShortOffsetTimeFormatter(Locale locale)
      Gets the formatter for a short OffsetTime.
      Parameters:
      locale - the locale
      Returns:
      the immutable and thread-safe formatter
    • determineScale

      public static int determineScale(String value)
      Determines the scale from a given numeric string.
      The scale is the number of digits after the decimal separator of the current locale.

      Does not work with scientific number strings.

      Parameters:
      value - the numeric string, may be null
      Returns:
      the scale, 0 if value is null or has no decimal separator
    • setScale

      public static void setScale(DecimalFormat format, int scale)
      Changes the decimal format according to the given scale.
      Leaves the part before the decimal point unchanged!
      Parameters:
      format - the decimal format
      scale - the scale
    • debitCreditToString

      public static String debitCreditToString(BMoney money, boolean debit)
      Translates a money value to a string with a suffix indicating whether debit or credit. Commonly used in financial accounting applications.
      Parameters:
      money - the amount of money
      debit - true if the amount is debit, else credit
      Returns:
      the formatted value, the empty string if money is null
    • isFormattingTime

      public static boolean isFormattingTime(String pattern)
      Checks whether the format pattern contains formatting codes for time.
      Parameters:
      pattern - the format pattern
      Returns:
      true if contains time-formatting
    • isFormattingDate

      public static boolean isFormattingDate(String pattern)
      Checks whether the format pattern contains formatting codes for date.
      Parameters:
      pattern - the format pattern
      Returns:
      true if contains date-formatting
    • isFormattingOffset

      public static boolean isFormattingOffset(String pattern)
      Checks whether the format pattern contains formatting codes for the zone offset.
      Parameters:
      pattern - the format pattern
      Returns:
      true if contains zone-offset formatting
    • isFormattingTimeZone

      public static boolean isFormattingTimeZone(String pattern)
      Checks whether the format pattern contains formatting codes for the timezone.
      Parameters:
      pattern - the format pattern
      Returns:
      true if contains timezone formatting
    • calendarFieldToString

      public static String calendarFieldToString(int field, boolean plural)
      Translates a field of Calendar to a localized string.

      Example: Calendar.DAY_OF_MONTH will be translated to "day".

      Notice that only the following field types are supported:

      Parameters:
      field - the field type
      plural - true if plural, else singular
      Returns:
      the name, the empty string if unsupported field type