TimeFormatter

open class TimeFormatter

The TimeFormatter class is a useful tool class to format in different ways the "time"



        // Get an instance of the TimeFormatter
        TimeFormatter timeFormatter = TimeFormatter.getInstance();

        // Change the values of the formatter
        timeFormatter.changeFormatter("new_pattern", new_locale);

        // or pass pattern as argument in all methods to change only in that formatting
        String stringDate = timeFormatter.formatNowAsString("yyyy/MM/dd HH:mm:ss"); // --> 2024/04/14 13:46:30

        // string format
        String stringDate = timeFormatter.formatAsString(time_in_millis); // --> 2024/04/14 13:46:30

        // long format
        System.out.println(timeFormatter.formatAsTimestamp("string_date")); // --> 1670261677000

        // date format
        // with string as argument
        System.out.println(timeFormatter.formatAsDate("string_date")); // --> Sun Apr 14 13:46:30 CET 2024

        // with long as argument
        System.out.println(timeFormatter.formatAsDate(time_in_millis); // --> Sun Apr 14 13:46:30 CET 2024
    

Author

N7ghtm4r3 - Tecknobit

Constructors

Link copied to clipboard
private constructor()
Constructor to init the TimeFormatter No-any params required
private constructor(pattern: String)
Constructor to init the TimeFormatter
private constructor(locale: Locale)
Constructor to init the TimeFormatter
private constructor(pattern: String, locale: Locale)
Constructor to init the TimeFormatter

Properties

Link copied to clipboard
val DEFAULT_PATTERN: String = "dd/MM/yyyy HH:mm:ss"
DEFAULT_PATTERN the default pattern used for the formatter
Link copied to clipboard
private open var formatter: SimpleDateFormat
formatter the formatter used to format the values
Link copied to clipboard
private open var locale: Locale
locale the locale for the formatter
Link copied to clipboard
private open var pattern: String
pattern the pattern for the formatter

Functions

Link copied to clipboard
open fun changeFormatter(newPattern: String, newLocale: Locale)
This method is used to set a new pattern for the formatter instance
Link copied to clipboard
open fun changeLocale(newLocale: Locale)
This method is used to set a new locale for the formatter instance
Link copied to clipboard
open fun changePattern(newPattern: String)
This method is used to set a new pattern for the formatter instance
Link copied to clipboard
open fun formatAsDate(date: String): Date
open fun formatAsDate(date: String, pattern: String): Date
This method is used to format a string-date as date
open fun formatAsDate(date: Long): Date
open fun formatAsDate(date: Long, pattern: String): Date
This method is used to format a timestamp value as date
Link copied to clipboard
open fun formatAsNowString(pattern: String): String
This method is used to format the current timestamp as string
Link copied to clipboard
open fun formatAsString(timestamp: Long): String
open fun formatAsString(timestamp: Long, pattern: String): String
This method is used to format a timestamp as string
Link copied to clipboard
open fun formatAsTimestamp(date: String): Long
open fun formatAsTimestamp(date: String, pattern: String): Long
This method is used to format a string-date value as timestamp
open fun formatAsTimestamp(date: Date): Long
open fun formatAsTimestamp(date: Date, pattern: String): Long
This method is used to format a date value as timestamp
Link copied to clipboard
open fun formatNowAsDate(): Date
This method is used to format the current timestamp as a date No-any params required
open fun formatNowAsDate(pattern: String): Date
This method is used to format the current timestamp as a date
Link copied to clipboard
This method is used to format the current timestamp as string No-any params required
Link copied to clipboard
This method is used to format the current instant as timestamp No-any params required
Link copied to clipboard
Method to get an instance of TimeFormatter No-any params required
open fun getInstance(pattern: String): TimeFormatter
open fun getInstance(locale: Locale): TimeFormatter
open fun getInstance(pattern: String, locale: Locale): TimeFormatter
Method to get an instance of TimeFormatter