String fomat method date conversion

Source: Internet
Author: User

1. syntax for regular, character, and numeric format specifiers: % [argument_index $] [flags] [width] [. Precision] Conversion

The optional argument_index is a decimal integer used to indicate the position of a parameter in the parameter list. The first parameter is referenced by "1 ___ fckpd ___ 0 quot;, and the second parameter is referenced by" 2 ___ fckpd ___ 0 quot;, and so on.

The optional flags are character sets that modify the output format. The set of valid flag depends on the Conversion Type.

Optional width is a non-negative decimal integer, indicating the minimum number of characters to write to the output.

The optional precision is a non-negative decimal integer, which is usually used to limit the number of characters. The specific action depends on the Conversion Type.

The required conversion is a character that indicates how to format parameters. The valid conversion set of a given parameter depends on the Data Type of the parameter.

1. Format an integer. If the number of digits is not enough, add 0 (\ d integers can only be 10 characters in length)

String. Format ("% 010d", 123) is converted to 0000000123

2. Format a floating point number. If the integer is not enough, add 0 to the forward, and if the decimal point is not enough, add 0 to the backward.

String. format ("% 010.3f", new bigdecimal (123.0 )). replace (". "," "); (Note that if the length is 9 bits. the length must be set to 10 (because there is. number)

3. Format a string. Fill in the blank space after the digits are not enough.

String. Format ("% 10 s", "ABC") (note that this method cannot be used to format Chinese characters)

Use the following methods to format Chinese characters:

String content = "Java formatting ";

String blank = ""; // space with a certain length

(Content + blank). substring (0, 20-content. getbytes (). Length );

2. format the date (the suffix characters are defined for conversion between 'T' and 'T)

String. Format ("% ty", new date () /// 2011

String. Format ("% TM", new date () // 03

String. Format ("% TF", new date () // 2011-03-04

String. Format ("% TR", new date () //

String. Format ("% TT", new date () // 15:49:34

String. Format ("% TC", new date () // Friday March 04 15:49:34 CST 2011

String. Format ("% TD", new date () // 03/04/11

String. Format ("% TD", new date () // 04

The following Conversion characters are used to format the date:
'B' is the full name of the month specific to the language environment, such as "January" and "February ".

'B' indicates the abbreviation of the month specific to the language environment, such as "Jan" and "FEB ".

'H' is the same as 'B.

'A is the full name of the day of the week specific to the language environment, such as "Sunday" and "Monday"

'A refers to the day of the week (such as "sun" and "mon") specific to the language environment"

'C' is divided by four digits of 100 to indicate the year. It is formatted as two digits with leading zeros (00-99) if necessary.

'Y' year, formatted as a four-digit (at least) with a leading zero when necessary, for example, 0092 equals 92 CE of the Gregorian calendar.

The last two digits of the 'y' year are formatted as the two digits with the leading zero when necessary, that is, 00-99.

The number of days in a year of 'J' is formatted as a three-digit number with a leading zero when necessary. For example, for a Gregorian calendar, the value is 001-366.

The 'M' month is formatted as a two-digit with a leading zero when necessary, that is, 01-13.

'D' the number of days in a month, which is formatted as the first zero or two digits (01-31) if necessary.

The number of days in a month is formatted as two digits, that is, 1-31.
The following Conversion characters are used to format common date/time combinations.
'R' 24-hour time, formatted as "% TH: % TM"

The time in the 'T' 24-hour format is formatted as "% TH: % TM: % ts ".

'R' 12-hour time, formatted as "% Ti: % TM: % TS % TP ". The location marked in the morning or afternoon ('% TP') may be related to the language environment.

'D' date, formatted as "% TM/% TD/% ty ".

'F' complete date in ISO 8601 format, formatted as "% ty-% TM-% TD ".

'C' date and time, formatted as "% TA % TB % TD % TT % TZ % ty", for example, "Sun Jul 20 16:17:00 EDT 1969 ".

String fomat method date conversion

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.