Java - Date Time Formatting

Introduction

Date/time formatting formats date, time, and datetime.

It can be applied to format values of long, Long, java.util.Calendar, java.util.Date, and java.time.temporal.TemporalAccessor types.

The value in a long/Long type argument is interpreted as the milliseconds passed since January 1, 1970 midnight UTC.

The 't' conversion character is used to format date/time values.

It has an uppercase variant 'T'.

Syntax

The general syntax for a format specifier for date/time formatting is as follows:

%<argument_index$><flags><width><conversion>

precision is not applicable to date/time formatting.

For date/time formatting, the conversion is a two-character sequence.

The first character in the conversion is always 't' or 'T'.

The second character is called the conversion suffix, which determines the format of the date/time argument.

The following table lists all the conversion suffixes that can be used with 't'/ 'T' data/time conversion character.

List of Suffix Characters for Time Formatting

Conversion Suffix
Description
'H'
A two-digit hour of the day for the 24-hour clock. The valid values are 00 to 23. 00 is used for midnight.
'I'

A two-digit hour of the day for the 12-hour clock. The valid values are 01 to 12.
01 is one o'clock in the morning or afternoon.
'k'

same as the 'H' suffix except that it does not add a leading zero to the output.
Valid values are 0 to 23.
'l'

same as 'I' suffix except that it does not add a leading zero.
Valid values are 1 to 12.
'M'
A two-digit minute within an hour. Valid values are 00 to 59.
'S'
A two-digit second within a minute. Valid values are 00 to 60. 60 is leap second.
'L'
A three-digit millisecond within a second. Valid values are 000 to 999.
'N'
A nine-digit nanosecond within a second. The valid values are 000000000 to 999999999.
'p'


a locale-specific morning or afternoon marker in lowercase. For example, for US
locale, it will output "am" or "pm". To output in uppercase (e.g., "AM" and "PM"
for US locale), use the uppercase variant 'T' as the conversion character..
'z'
outputs the numeric time zone offset from GMT (e.g., +0530).
'Z'
a string abbreviation of the time zone (e.g., CST, EST, IST, etc).
's'
outputs seconds since the beginning of the epoch starting at January 1, 1970 midnight UTC.
'Q'

outputs milliseconds since the beginning of the epoch starting at January 1, 1970
midnight UTC.

List of Suffix Characters for Date Formatting

Conversion Suffix
Description
'B'
Locale-specific full name of the month, such as "January", "February", etc. for US locale.
'b'
Locale-specific abbreviated month name, such as "Jan", "Feb", etc. for US locale.
'h'
Same as 'b'
'A'
Locale-specific full name of the day of the week, such as "Sunday", "Monday", etc. for US locale.
'a'
Locale-specific short name of the day of the week, such as "Sun", "Mon", etc. for US locale.
'C'


It divides the four-digit year by 100 and formats the result as two digits. It adds a leading zero if
the resulting number is one digit. Valid values are 00 to 99.
For example, if the four-digit year is 2011, it will output 20; if the four-digit year is 12, it will output 00.
'Y'
At least a four-digit year. It adds leading zeros if the year contains less than four digits.
'y'

The last two digits of the year. It adds leading zero if necessary. For example, if the year is 9, it
will output 09; if the year is 123, it will output 23; if the year is 2011, it will output 11.
'j'
A three-digit day of the year. Valid values are 000 to 366.
'm'
A two-digit month. Valid values are 01 to 13. 13 is required to support lunar calendar.
'd'
A two-digit day of the month. Valid values are 01 to 31.
'e'

Day of the month. Valid values are 1 to 31. It behaves the same as 'd' except that it does not add
a leading zero to the output.

List of Suffix Characters for Date/Time Formatting

Conversion
Suffix
Description

'R'

It formats time in 24-hour clock format as "hour:minute". Its effects is the same as using
"%tH:%tM" as a format specifier. Examples are "11:12", "01:35", "21:30", etc.
'T'

It formats time in 24-hour clock format as "hour:minute:second". Its effects is the same as using
"%tH:%tM:%tS" as a format specifier. Examples are "11:13:10", "01:35:01", "21:30:34", etc.
'r'


It formats time in 12-hour clock format as "hour:minute:second morning/afternoon marker".
Its effects is the same as using "%tI:%tM:%tS %Tp" as a format specifier. The morning/afternoon
marker may be locale-specific. "09:23:45 AM", "09:30:00 PM", etc. are example for US locale.
'D'
It formats the date as "%tm/%td/%ty", such as "01/19/11".
'F'
It formats the date as "%tY-%tm-%td", such as "2018-01-19".
'c'
It formats the date and time as "%ta %tb %td %tT %tZ %tY", such as "Wed Jan 19 11:52:06 CST 2011".

The data/time conversion applies localization wherever it is applicable.

The following code formats the same date and time, January 25, 2018 11:48:16 AM, in US, Indian, and Thai locales.

Demo

import java.time.LocalDateTime;
import java.time.Month;
import java.util.Locale;

public class Main {
  public static void main(String[] args) {
    Locale englishUS = Locale.US;
    Locale hindiIndia = new Locale("hi", "IN");
    Locale thaiThailand = new Locale("th", "TH", "TH");

    // Construct a LocalDateTime
    LocalDateTime ldt = LocalDateTime.of(2018, Month.JANUARY, 25, 11, 48, 16);

    System.out.printf(englishUS, "In US: %tB %<te, %<tY %<tT %<Tp%n", ldt);
    System.out.printf(hindiIndia, "In India: %tB %<te, %<tY %<tT %<Tp%n", ldt);
    System.out.printf(thaiThailand, "In Thailand: %tB %<te, %<tY %<tT %<Tp%n",
        ldt);/* w ww  . j av  a 2 s  . c  o m*/

  }
}

Result

Related Topics