Conversion characters for time in Java

Java 8Object Oriented ProgrammingProgramming

The following are the conversion characters for date-time.

CharacterDescription
cComplete date and time
FISO 8601 date
DU.S. formatted date (month/day/year)
T24-hour time
r12-hour time
R24-hour time, no seconds
YFour-digit year (with leading zeroes)
yLast two digits of the year (with leading zeroes)
CFirst two digits of the year (with leading zeroes)
BFull month name
bAbbreviated month name
mTwo-digit month (with leading zeroes)
dTwo-digit day (with leading zeroes)
eTwo-digit day (without leading zeroes)
AFull weekday name
aAbbreviated weekday name
jThree-digit day of year (with leading zeroes)
HTwo-digit hour (with leading zeroes), between 00 and 23
kTwo-digit hour (without leading zeroes), between 0 and 23
ITwo-digit hour (with leading zeroes), between 01 and 12
lTwo-digit hour (without leading zeroes), between 1 and 12
MTwo-digit minutes (with leading zeroes)
STwo-digit seconds (with leading zeroes)
LThree-digit milliseconds (with leading zeroes)
NNine-digit nanoseconds (with leading zeroes)
PUppercase morning or afternoon marker
pLowercase morning or afternoon marker
zRFC 822 numeric offset from GMT
ZTime zone
sSeconds since 1970-01-01 00:00:00 GMT
QMilliseconds since 1970-01-01 00:00:00 GMT

The following is an example.

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main( String args[] ) {
      Calendar cal = Calendar.getInstance();
      // date
      System.out.printf("Date....\n");
      System.out.printf( "%1$tA, %1$tB %1$td, %1$tY\n", cal );
      System.out.printf( "%1$ta, %1$tb %1$te, %1$ty\n", cal );
      // time
      System.out.printf("\nTime....\n");
      System.out.printf( "%1$tH:%1$tM:%1$tS\n", cal );
      System.out.printf( "%1$tZ %1$tI:%1$tM:%1$tS", cal );
   }
}

Output

Date....
Monday, November 26, 2018
Mon, Nov 26, 18
Time....
12:41:28
UTC 12:41:28
raja
Published on 14-Dec-2018 11:35:44
Advertisements