This field is transferred from CSDN 1989
This article is a summary of the study of the article on the network, thank you for your selfless sharing.
The System.out.printf () method can handle the output of a date.
Corresponding List
Conversion character |
Type |
Example |
C |
The full date and time |
Mon Feb 18:05:11 PST 2004 |
F |
ISO 8061 Date |
2004-02-09 |
D |
Date in US format |
02/09/2004 |
T |
24 Hour time |
18:03:23 |
R |
12 Hour time |
05:03:23 pm |
R |
24 hours, no seconds. |
18:05 |
Y |
4-digit year (front complement 0) |
2004 |
Y |
The following two digits of the year (front 0) |
04 |
C |
First two digits of the year (front 0) |
20 |
B |
Full spelling of the month |
February |
B or H |
Abbreviation of the Month |
Feb |
M |
Two-digit month (preceded by 0) |
02 |
D |
Two-digit day (front 0) |
09 |
E |
Two-digit month (not 0 in front) |
9 |
A |
Full spelling of the day of the week |
Monday |
A |
Abbreviation of the day of the week |
Mon |
J |
Three-digit years in the year (front 0), between 001 and 366 |
16X |
H |
Two-digit hour (front 0), between 0 and 23 |
18 |
K |
Two-digit hour (front not 0) between 0 and 23 |
8 |
I (Uppercase i) |
Two-digit hour (front 0), between 0 and 12 |
06 |
L (lowercase L) |
Two-digit hour (front not 0), between 0 and 12 |
6 |
M |
Two-digit minute (front 0) |
05 |
S |
Two-digit seconds (front-fill 0) |
19 |
L |
Three-digit millisecond (preceded by 0) |
047 |
N |
Nine-digit nanosecond (preceded by 0) |
047000000 |
P |
Morning or afternoon capital sign |
Pm |
P |
Morning or afternoon lowercase flag |
Pm |
Z |
From GMT, RFC822 digital Shift |
-0800 |
Z |
Time |
Pst |
S |
Number of seconds from GMT 1970-01-01 00:00:00 |
107884319 |
Q |
Number of milliseconds since GMT 1970-01-01 00:00:01 |
107884319047 |
Test code
PackageSE;Importjava.util.Date;/*** * <p> * Description: This example is to learn printf formatted output for date type * </p> * *@authorZhangjunshuai *@version1.0 Create date:2014-10-16 pm 6:12:50 Project name:java7thread * * <pre> * * Modification History: * Date Author Version Description *---------- -------------------------------------------------------------------------------------------------* Lastchange: $ Date:: $ $Author: $ $Rev: $ * </pre> **/ Public classPrintfdate {/*** <p> * </p> * *@authorZhangjunshuai * @date 2014-10-16 pm 6:12:46 *@paramargs*/ Public Static voidMain (string[] args) {//The printf method can print the time format, starting with T, in two-letter format where people end with letters in a tableSystem.out.printf ("%tc\n",NewDate ()); System.out.printf ("%tf\n",NewDate ()); System.out.printf ("%td\n",NewDate ()); System.out.printf ("%tt\n",NewDate ()); System.out.printf ("%tr\n",NewDate ()); System.out.printf ("%tr\n",NewDate ()); System.out.printf ("%ty\n",NewDate ()); System.out.printf ("%ty\n",NewDate ()); System.out.printf ("%tc\n",NewDate ()); System.out.printf ("%tb\n",NewDate ()); System.out.printf ("%tm\n",NewDate ()); System.out.printf ("%td\n",NewDate ()); System.out.printf ("%te\n",NewDate ()); System.out.printf ("%ta\n",NewDate ()); System.out.printf ("%ta\n",NewDate ()); System.out.printf ("%tj\n",NewDate ()); System.out.printf ("%th\n",NewDate ()); System.out.printf ("%tk\n",NewDate ()); System.out.printf ("%ti\n",NewDate ()); System.out.printf ("%tl\n",NewDate ()); System.out.printf ("%tm\n",NewDate ()); System.out.printf ("%ts\n",NewDate ()); System.out.printf ("%tl\n",NewDate ()); System.out.printf ("%tn\n",NewDate ()); System.out.printf ("%tp\n",NewDate ()); //System.out.printf ("%tp\n", New Date ());//This method error should be related to time zoneSystem.out.printf ("%tz\n",NewDate ()); System.out.printf ("%tz\n",NewDate ()); System.out.printf ("%ts\n",NewDate ()); System.out.printf ("%tq\n",NewDate ()); }}
Results:
As you can see from the table above, some things only give partial information for a specified date, and it is too clumsy to have to do a part of the operation on a date operation multiple times, so that you can use a formatted parameter index for formatting the string. The index must be immediately after the%, ending with $. The index starts at 1, not 0.
- System.out.printf ("%1 $ s%2$TB%2$te,%2$ty\n", "Due Date:", new Date ()); % after 2 represents the second parameter, here is the new Date ()
You can also choose to use the < flag, which indicates that the parameters in the preceding format description will be used again.
- System.out.printf ("%s%tb%<te,%<ty", "Due Date:", new Date ()); Select Use < Flag, which indicates that the parameters in the preceding format description will be used again.
The date () Conversion of Java