Topics Index

More Utility Classes > Formatter >
Siva Nookala - 06 Apr 2016
Formatting Time and Date is done using conversion specifier %t. %t requires a suffix to describe the portion and precise format of the time or date desired. The argument corresponding to the %t must be of the type Calendar, Date, Long, orlong.

The following table gives us the description of the suffixes of %t:
Suffix Replaced By
aAbbreviated weekday name
AFull weekday name
bAbbreviated month name
BFull month name
cStandard date and time string formatted as day month date hh::mm::ss tzone year
CFirst two digits of a year
dDay of month as decimal(01-31)
eDay of month as decimal(1-31)
hAbbreviated month name
HHour(00 to 23)
IHour(01 to 12)
jDay of year as a decimal (001 to 366)
kHour(0 to 23)
lHour(1 to 12)
LMillisecond(000 to 999)
mMonth as decimal(01 to 13)
MMinute as decimal(00 to 59)
NNanosecond(000000000 to 999999999)
pLocale's equivalent of AM or PM in lower case
QMilliseconds from 1/1/1970
rhh:mm:ss(12-hour format)
Rhh:mm(24-hour format)
SSeconds(00 to 60)
sSeconds from 1/1/1970 UTC
Thh:mm:ss(24-hour format)
yYear in decimal without century (00 to 99)
YYear in decimal with century (0001 to 9999)
zOffset from UTC
ZTime zone name
The following example program demonstrates several formats of %t:
Time and Date Formatter
import java.util.*;

class FormatTimeDate
    public static void main(String arg[])
        Calendar cal = Calendar.getInstance();
        Formatter fmt = new Formatter();
        fmt.format("%tc", cal);
        fmt = new Formatter();
        fmt.format("%tT", cal);
        fmt = new Formatter();
        fmt.format("%tF %tB %tA", cal, cal, cal);

Mon Jan 13 01:27:38 PST 2014
2014-01-13 January Monday


Note that the output of this program changes according to the date and time of the compilation. This is because we have used Calendar.getInstance() which returns the current date and time.

The first output line displays the standard date and time string formatted as day month date hh::mm::ss tzone year since we used %tc. The second output line is the display of hh:mm:ss (24-hour format) since we used %tT. The third output line is the display of year-month-day, full month name, full week-day name since we used %tF, %tB, %tA.

  • Try for all the suffixes mentioned in the table.
  • Instead ofCalendar, try to use Date, Long, or long as arguments for %t. Date can be created using new Date()

Dependent Topics : Java Formatter Class   Java Formatter Methods  

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.