Java SE Learning the printf date conversion character

Source: Internet
Author: User
Tags iso 8061 java se

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 069
H Two-digit hour (preceded by 0). from 0 to 23 18
K Two-digit hour (front not 0) between 0 and 23 8
I (Uppercase i) Two-digit hour (preceded by 0). from 0 to 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) 14V
N Nine-digit nanosecond (preceded by 0) 047000000
P Morning or afternoon capital sign Pm
P Morning or afternoon lowercase flag Pm
Z Starting 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

Package Se;import java.util.date;/** * * <p> * Description: This example is to learn printf formatted output for date type * </p> * @author ZHANGJ Unshuai * @version 1.0 * Create date:2014-10-16 PM 6:12:50 * Project name:java7thread * * <pre> * Modification Hist Ory: * Date Author Version Description *--------  ---------------------------------------------------------------------------------------------------* Lastchange: $Date:: $ $Author: $ $Rev: $ * </pre> * */public class Printfdate {/** * &LT;P&G T * </p> * @author Zhangjunshuai * @date 2014-10-16 pm 6:12:46 * @param args */public static void main (string[] args) { The printf method can print the time format. Starting with T, the two-letter format System.out.printf ("%tc\n", New Date ()) in which the person ends with a letter in the table; System.out.printf ("%tf\n", New Date ()); System.out.printf ("%td\n", New Date ()); System.out.printf ("%tt\n", New Date ()); System.out.printf ("%tr\n", New Date ()); System.out.printf ("%tr\n", New Date ()); System.out.printf ("%ty\n", New Date ()); System.out.printf ("%ty\n", New Date ()); System.out.printf ("%tc\n", New Date ()); System.out.printf ("%tb\n", New Date ()); System.out.printf ("%tm\n", New Date ()); System.out.printf ("%td\n", New Date ()); System.out.printf ("%te\n", New Date ()); System.out.printf ("%ta\n", New Date ()); System.out.printf ("%ta\n", New Date ()); System.out.printf ("%tj\n", New Date ()); System.out.printf ("%th\n", New Date ()); System.out.printf ("%tk\n", New Date ()); System.out.printf ("%ti\n", New Date ()); System.out.printf ("%tl\n", New Date ()); System.out.printf ("%tm\n", New Date ()); System.out.printf ("%ts\n", New Date ()); System.out.printf ("%tl\n", New Date ()); System.out.printf ("%tn\n", New Date ()); System.out.printf ("%tp\n", new Date ()),//system.out.printf ("%tp\n", new Date ());//This method error should be related to time zone System.out.printf (" %tz\n ", New Date ()); System.out.printf ("%tz\n", New Date ()); System.out.printf ("%ts\n", New Date ()); System.out.printf ("%tq\n", New Date ());}}
Results:



Can be seen from the above table. Some things just give some information about a given date. It is too clumsy to assume that a part of the operation of a date operation can be accomplished several times. This enables the use of a formatted parameter index at the point where a formatted string is used.

The index must be immediately after the%, ending with $. index starts from 1 , rather than 0.

System.out.printf ("%1 $ s%2$TB%2$te,%2$ty\n", "Due Date:", new Date ()), and 2 after//% represents the second number of parameters. This refers to the new Date ()

can also choose to use the < logo. It 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, It indicates that the parameters in the preceding format description will be used again. 


Java se learning printf date conversion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.