Class Formatters

java.lang.Object
com.oorian.i18n.Formatters

public final class Formatters extends Object
Locale-aware formatting utilities for dates, times, numbers, and currencies.

All methods that do not take an explicit Locale parameter use OorianLocale.get() to determine the active locale. Methods are thread-safe and stateless.

Usage:


 OorianLocale.set(Locale.GERMANY);

 Formatters.formatDate(LocalDate.now());         // "20.02.2026"
 Formatters.formatNumber(1234567.89);            // "1.234.567,89"
 Formatters.formatCurrency(49.99, Currency.getInstance("EUR")); // "49,99 €"
 Formatters.formatPercent(0.85);                 // "85 %"
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Details

    • formatDate

      public static String formatDate(LocalDate date)
      Formats a date using the MEDIUM style and the current locale.
      Parameters:
      date - the date to format
      Returns:
      the formatted date string, or an empty string if date is null
    • formatDate

      public static String formatDate(LocalDate date, FormatStyle style)
      Formats a date using the specified style and the current locale.
      Parameters:
      date - the date to format
      style - the format style (SHORT, MEDIUM, LONG, FULL)
      Returns:
      the formatted date string, or an empty string if date is null
    • formatDate

      public static String formatDate(LocalDate date, FormatStyle style, Locale locale)
      Formats a date using the specified style and locale.
      Parameters:
      date - the date to format
      style - the format style
      locale - the locale to use
      Returns:
      the formatted date string, or an empty string if date is null
    • formatDateTime

      public static String formatDateTime(LocalDateTime dateTime)
      Formats a date-time using the MEDIUM style and the current locale.
      Parameters:
      dateTime - the date-time to format
      Returns:
      the formatted date-time string, or an empty string if dateTime is null
    • formatDateTime

      public static String formatDateTime(LocalDateTime dateTime, FormatStyle style)
      Formats a date-time using the specified style and the current locale.
      Parameters:
      dateTime - the date-time to format
      style - the format style
      Returns:
      the formatted date-time string, or an empty string if dateTime is null
    • formatDateTime

      public static String formatDateTime(LocalDateTime dateTime, FormatStyle style, Locale locale)
      Formats a date-time using the specified style and locale.
      Parameters:
      dateTime - the date-time to format
      style - the format style
      locale - the locale to use
      Returns:
      the formatted date-time string, or an empty string if dateTime is null
    • formatTime

      public static String formatTime(LocalTime time)
      Formats a time using the MEDIUM style and the current locale.
      Parameters:
      time - the time to format
      Returns:
      the formatted time string, or an empty string if time is null
    • formatTime

      public static String formatTime(LocalTime time, FormatStyle style)
      Formats a time using the specified style and the current locale.
      Parameters:
      time - the time to format
      style - the format style
      Returns:
      the formatted time string, or an empty string if time is null
    • formatTime

      public static String formatTime(LocalTime time, FormatStyle style, Locale locale)
      Formats a time using the specified style and locale.
      Parameters:
      time - the time to format
      style - the format style
      locale - the locale to use
      Returns:
      the formatted time string, or an empty string if time is null
    • formatNumber

      public static String formatNumber(Number value)
      Formats a number using the current locale's default number format.
      Parameters:
      value - the number to format
      Returns:
      the formatted number string, or an empty string if value is null
    • formatNumber

      public static String formatNumber(Number value, Locale locale)
      Formats a number using the specified locale's default number format.
      Parameters:
      value - the number to format
      locale - the locale to use
      Returns:
      the formatted number string, or an empty string if value is null
    • formatNumber

      public static String formatNumber(Number value, int decimalPlaces)
      Formats a number with a fixed number of decimal places using the current locale.
      Parameters:
      value - the number to format
      decimalPlaces - the number of decimal places
      Returns:
      the formatted number string, or an empty string if value is null
    • formatNumber

      public static String formatNumber(Number value, int decimalPlaces, Locale locale)
      Formats a number with a fixed number of decimal places using the specified locale.
      Parameters:
      value - the number to format
      decimalPlaces - the number of decimal places
      locale - the locale to use
      Returns:
      the formatted number string, or an empty string if value is null
    • formatCurrency

      public static String formatCurrency(Number value, Currency currency)
      Formats a number as currency using the specified currency and the current locale.
      Parameters:
      value - the monetary amount
      currency - the currency (e.g., Currency.getInstance("USD"))
      Returns:
      the formatted currency string, or an empty string if value is null
    • formatCurrency

      public static String formatCurrency(Number value, Currency currency, Locale locale)
      Formats a number as currency using the specified currency and locale.
      Parameters:
      value - the monetary amount
      currency - the currency
      locale - the locale to use for formatting conventions
      Returns:
      the formatted currency string, or an empty string if value is null
    • formatPercent

      public static String formatPercent(Number value)
      Formats a number as a percentage using the current locale.

      The value is expected as a fraction (e.g., 0.85 for 85%).

      Parameters:
      value - the fractional value
      Returns:
      the formatted percentage string, or an empty string if value is null
    • formatPercent

      public static String formatPercent(Number value, Locale locale)
      Formats a number as a percentage using the specified locale.

      The value is expected as a fraction (e.g., 0.85 for 85%).

      Parameters:
      value - the fractional value
      locale - the locale to use
      Returns:
      the formatted percentage string, or an empty string if value is null
    • parseDate

      public static LocalDate parseDate(String text, FormatStyle style)
      Parses a locale-formatted date string into a LocalDate.
      Parameters:
      text - the text to parse
      style - the format style that was used to format the date
      Returns:
      the parsed date, or null if the text is null or empty
      Throws:
      DateTimeParseException - if the text cannot be parsed
    • parseDate

      public static LocalDate parseDate(String text, FormatStyle style, Locale locale)
      Parses a locale-formatted date string into a LocalDate.
      Parameters:
      text - the text to parse
      style - the format style
      locale - the locale to use
      Returns:
      the parsed date, or null if the text is null or empty
      Throws:
      DateTimeParseException - if the text cannot be parsed
    • parseDateTime

      public static LocalDateTime parseDateTime(String text, FormatStyle style)
      Parses a locale-formatted date-time string into a LocalDateTime.
      Parameters:
      text - the text to parse
      style - the format style
      Returns:
      the parsed date-time, or null if the text is null or empty
      Throws:
      DateTimeParseException - if the text cannot be parsed
    • parseDateTime

      public static LocalDateTime parseDateTime(String text, FormatStyle style, Locale locale)
      Parses a locale-formatted date-time string into a LocalDateTime.
      Parameters:
      text - the text to parse
      style - the format style
      locale - the locale to use
      Returns:
      the parsed date-time, or null if the text is null or empty
      Throws:
      DateTimeParseException - if the text cannot be parsed
    • parseNumber

      public static Number parseNumber(String text) throws ParseException
      Parses a locale-formatted number string.
      Parameters:
      text - the text to parse
      Returns:
      the parsed number, or null if the text is null or empty
      Throws:
      ParseException - if the text cannot be parsed
    • parseNumber

      public static Number parseNumber(String text, Locale locale) throws ParseException
      Parses a locale-formatted number string using the specified locale.
      Parameters:
      text - the text to parse
      locale - the locale to use
      Returns:
      the parsed number, or null if the text is null or empty
      Throws:
      ParseException - if the text cannot be parsed