Date and time conversions can be applied to java.util.Calendar and java.util.Date objects. They can also be applied to long andLongvalues, in which case the value is assumed to be the number of milliseconds since midnight, January 1, 1970. All date and time conversions begin with atfor lowercase or aTfor uppercase. These conversions are:
%tH/ %TH Two-digit hour using a 24-hour clock, ranging from 00 to 23
%tI/%TI Two-digit hour using a 12-hour clock, ranging from 01 to 12
%tk/ %Tk One- or two-digit hour using a 24-hour clock, ranging from 0 to 23
%tl/ %Tl One- or two-digit hour using a 12-hour clock, ranging from 1 to 12
%tM/ %TM Two-digit minutes, ranging from 00 to 59
%tS/ %TS Two-digit seconds, ranging from 00 to 60 (60 is used for leap seconds)
%tL/ %TL Three-digit milliseconds, ranging from 000 to 999
%tN/ %TN Nine-digit nanoseconds, ranging from 000000000 to 999999999
%tp/ %Tp Locale-specific morning/afternoon indicator, such as am or PM
%tz/ %Tz RFC 822 numeric time zone indicator as an offset from UMT (for instance, Eastern Standard Time is– 0500)
%tZ/ %TZ An abbreviation for the time zone, such as edt or EST
%ts/ %Ts Seconds elapsed since midnight, January 1, 1970, Greenwich Mean Time
%TQ Milliseconds elapsed since midnight, January 1, 1970, Greenwich Mean Time
%tB/ %TB Localized month, such as “January” or “JANVIER”
%tb/ %Tb Localized, abbreviated month, such as “Jan” or “JAN”
%th/ %Th Localized, abbreviated month, such as “Jan” or “JAN” (yes,%tband%thare synonyms; I have no idea why)
%tA/ %TA Localized day name, such as “Tuesday” or “MARDI”
%ta/ %Ta Localized, abbreviated day, such as “Tue” or “TUE”
%tC/ %TC Two-digit century, ranging from 00 to 99
%tY/ %TY Year with at least four digits, ranging from 0001 to the indefinite future
%ty/ %Ty Two-digit year, ranging from 00 to 99
%tj/ %Tj Three-digit day of the year, ranging from 001 to 366
%tm/ %Tm Two-digit month, ranging from 01 to 13 (13 is used in some non-Gregorian lunar calendars)
%td/ %Td Two-digit day of the month, ranging from 01 to 31
%te/ %Te One- or two-digit day of the month, ranging from 1 to 31
%tR/ %TR Hours and minutes on a 24-hour clock, such as 03:23 or 14:07
%tT/ %TT Hours, minutes, and seconds on a 24-hour clock, such as 03:23:17 or 14:07:00
%tr/ %Tr Hours, minutes, and seconds on a 12-hour clock, such as 03:23:17 am or 02:07:00 PM
%tD/ %TD Date in the form month/day/year, such as 05/12/06
%tF/ %TF ISO 8601 standard date in the form year-month-day, such as 2006-05-12
%tc/ %Tc Date and time formatted like so: “Fri May 12 12:27:30 EDT 2006”
Example 7-3 prints the current date and time in all of these formats.
Example 7-3. Date format specifiers
import java.util.Date;
public class DateFormatExample {
public static void main(String[] args) { Date now = new Date(); System.out.printf("two-digit hour on a 24-hour clock: %tH/%TH\n", now, now); System.out.printf("two-digit hour on a 12-hour clock: %tI/%TI\n", now, now); System.out.printf("one- or two-digit hour on a 24-hour clock: %tk/%Tk\n", now, now); System.out.printf("one- or two-digit hour on a 12-hour clock: %tl/%Tl\n", now, now); System.out.printf("two-digit minutes ranging from 00 to 59: %tH/%TH\n", now, now); System.out.printf("two-digit seconds ranging from 00 to 60 : %tS/%TS\n", now, now); System.out.printf("milliseconds: %tL/%TL\n", now, now); System.out.printf("nanoseconds: %tN/%TN\n", now, now); System.out.printf("locale-specific morning/afternoon indicator: %tp/%Tp\n", now, now); System.out.printf("RFC 822 numeric time zone indicator: %tz/%Tz\n", now, now); System.out.printf("time zone abbreviation: %tZ/%TZ\n", now, now); System.out.printf("seconds since the epoch: %ts/%Ts\n", now, now); System.out.printf("milliseconds since the epoch: %TQ\n", now); System.out.printf("localized month name: %tB/%TB\n", now, now); System.out.printf("localized, abbreviated month: %tb/%Tb\n", now, now); System.out.printf("localized, abbreviated month: %th/%Th\n", now, now); System.out.printf("localized day name: %tA/%TA\n", now, now); System.out.printf("localized, abbreviated day: %ta/%Ta\n", now, now); System.out.printf("two-digit century: %tC/%TC\n", now, now); System.out.printf("four-digit year: %tY/%TY\n", now, now); System.out.printf("two-digit year: %ty/%Ty\n", now, now); System.out.printf("three-digit day of the year: %tj/%Tj\n", now, now); System.out.printf("two-digit month: %tm/%Tm\n", now, now); System.out.printf("two-digit day of the month: %td/%Td\n", now, now); System.out.printf("one- or two-digit day of the month: %te/%Te\n", now, now); System.out.printf("hours and minutes on a 24-hour clock: %tR/%TR\n", now, now); System.out.printf("hours, minutes, and seconds on a 24-hour clock: %tT/%TT\n", now, now); System.out.printf("hours, minutes, and seconds on a 12-hour clock: %tr/%Tr\n", now, now); System.out.printf("month/day/year: %tD/%TD\n", now, now); System.out.printf("ISO 8601 standard date: %tF/%TF\n", now, now); System.out.printf("Unix date format: %tc/%Tc\n", now, now); } }
Here’s the output when this was run on Friday, June 24, 2005 at 6:43 PM EDT:
two-digit hour on a 24-hour clock: 18/18 two-digit hour on a 12-hour clock: 06/06 one- or two-digit hour on a 24-hour clock: 18/18 one- or two-digit hour on a 12-hour clock: 6/6 two-digit minutes ranging from 00 to 59: 18/18 two-digit seconds ranging from 00 to 60 : 50/50 milliseconds: 859/859 nanoseconds: 859000000/859000000 locale-specific morning/afternoon indicator: pm/PM RFC 822 numeric time zone indicator: -0500/-0500 time zone abbreviation: EDT/EDT seconds since the epoch: 1119653030/1119653030 milliseconds since the epoch: 1119653030859 localized month name: June/JUNE localized, abbreviated month: Jun/JUN localized, abbreviated month: Jun/JUN localized day name: Friday/FRIDAY localized, abbreviated day: Fri/FRI two-digit century: 20/20 four-digit year: 2005/2005 two-digit year: 05/05 three-digit day of the year: 175/175 two-digit month: 06/06 two-digit day of the month: 24/24 one- or two-digit day of the month: 24/24 hours and minutes on a 24-hour clock: 18:43/18:43 hours, minutes, and seconds on a 24-hour clock: 18:43:50/18:43:50 hours, minutes, and seconds on a 12-hour clock: 06:43:50 PM/06:43:50 PM month/day/year: 06/24/05/06/24/05 ISO 8601 standard date: 2005-06-24/2005-06-24 Unix date format: Fri Jun 24 18:43:50 EDT 2005/FRI JUN 24 18:43:50 EDT 2005