Date Formatting Using printf


Date and time formatting can be done very easily using the printf method. You use a two-letter format, starting with t and ending in one of the letters of the table as shown in the following code.

Example

Live Demo

import java.util.Date;
public class DateDemo {

   public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date();

      // display time and date
      String str = String.format("Current Date/Time : %tc", date );

      System.out.printf(str);
   }
}

This will produce the following result −

Output

Current Date/Time : Sat Dec 15 16:37:57 MST 2012

It would be a bit silly if you had to supply the date multiple times to format each part. For that reason, a format string can indicate the index of the argument to be formatted.

The index must immediately follow the % and it must be terminated by a $.

Example

Live Demo

import java.util.Date;
public class DateDemo {

   public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date();

      // display time and date
      System.out.printf("%1$s %2$tB %2$td, %2$tY", "Due date:", date);
   }
}

This will produce the following result −

Output

Due date: February 09, 2004

Alternatively, you can use the < flag. It indicates that the same argument as in the preceding format specification should be used again.

Example

Live Demo

import java.util.Date;
public class DateDemo {

   public static void main(String args[]) {
      // Instantiate a Date object
      Date date = new Date();

      // display formatted date
      System.out.printf("%s %tB %<te, %<tY", "Due date:", date);
   }
}

This will produce the following result −

Output

Due date: February 09, 2004

Date and Time Conversion Characters

Character
Description
Example
c
Complete date and time
Mon May 04 09:51:52 CDT 2009
F
ISO 8601 date
2004-02-09
D
U.S. formatted date (month/day/year)
02/09/2004
T
24-hour time
18:05:19
r
12-hour time
06:05:19 pm
R
24-hour time, no seconds
18:05
Y
Four-digit year (with leading zeroes)
2004
y
Last two digits of the year (with leading zeroes)
04
C
First two digits of the year (with leading zeroes)
20
B
Full month name
February
b
Abbreviated month name
Feb
m
Two-digit month (with leading zeroes)
02
d
Two-digit day (with leading zeroes)
03
e
Two-digit day (without leading zeroes)
9
A
Full weekday name
Monday
a
Abbreviated weekday name
Mon
j
Three-digit day of the year (with leading zeroes)
069
H
Two-digit hour (with leading zeroes), between 00 and 23
18
k
Two-digit hour (without leading zeroes), between 0 and 23
18
I
Two-digit hour (with leading zeroes), between 01 and 12
06
l
Two-digit hour (without leading zeroes), between 1 and 12
6
M
Two-digit minutes (with leading zeroes)
05
S
Two-digit seconds (with leading zeroes)
19
L
Three-digit milliseconds (with leading zeroes)
047
N
Nine-digit nanoseconds (with leading zeroes)
047000000
P
Uppercase morning or afternoon marker
PM
p
Lowercase morning or afternoon marker
pm
z
RFC 822 numeric offset from GMT
-0800
Z
Time zone
PST
s
Seconds since 1970-01-01 00:00:00 GMT
1078884319
Q
Milliseconds since 1970-01-01 00:00:00 GMT
1078884319047

Updated on: 19-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements