Java uses SimpleDateFormat to format date time. Rp

Source: Internet
Author: User

First look at all the representations of the formatted log.

[Plain]View plain copy
    1. Time Date Identifier:
    2. YYYY: Year
    3. MM: Month
    4. DD: Day
    5. HH:1~12-hour System (1-12)
[Plain]View plain copy
    1. HH:24-hour System (0-23)
    2. MM: Min
    3. SS: Seconds
    4. S: MS
    5. E: Day of the week
    6. D: The day ordinal of a year
    7. F: The first few weeks of January (the total number of days in this month divided by 7)
    8. W: The first few weeks of the year
    9. W: The week of January (will be calculated according to the actual situation)
    10. A: Last afternoon logo
    11. K: Similar to HH, indicating a 24-hour day (1-24).
    12. K: And Hh<span > Almost </span><span, which represents a 12-hour day (0-11). </span>
    13. Z: Indicates time zone

Be aware of the size of the case, or it will be formatted after the date will be very messy Oh!!!

At the same time not a letter can be written more than, such as: yyyyyyyyyyy. According to your number size, according to the length of the letter written, in front of the 0.

For example: you want to put 2013-08-13 15:29:44 this time format to August 13, 2013 15:29 44 seconds

Accidentally mistake the case in the time identifier in the formatted date.

[Java]View plain copy
    1. SimpleDateFormat SDF = new SimpleDateFormat ("yyyy mm month dd Day hh mm min ss sec");
    2. It's written like this. The final result is wrong.
[Java]View plain copy
    1. 2013 August 14th 15 o'clock 08 minutes 59 seconds
[Java]View plain copy
    1. This mm is written here is the month of the expression. And the minute is the expression of mm. So pay attention to,


The following is the most complete example of a date format word.

[Java]View plain copy
    1. SimpleDateFormat SDF = new SimpleDateFormat ("Yyyyyyyy-mm-dd hh (hh): Mm:ss S E D F w w a k K z");
    2. Long times = System.currenttimemillis ();
    3. System.out.println (times);
    4. Date date = new Date (times);
    5. String Tim = Sdf.format (date);
    6. System.out.println (Tim);

Results of the output:

[HTML]View plain copy
    1. 00002013-08-14 15 (03): 56:40 742 weeks 3,226 2 33 3 pm 3 CST

[Java]View plain copy
  1. SimpleDateFormat SDF = new SimpleDateFormat ("Now is the yyyy mm month DD Day hh (HH) mm Sub SS s millisecond Week E this year D Day this month the first F week this year the first W star  Period of the month of the first W week of today's a k1~24 system time k0-11 hours time Z time zone ");
  2. Long times = System.currenttimemillis ();
  3. System.out.println (times);
  4. Date date = new Date (times);
  5. String Tim = Sdf.format (date);
  6. System.out.println (Tim);


Output Result:

[HTML]View plain copy
    1. Now is August 14, 2013 16 (04) when 03 minutes 03 seconds 812 milliseconds Week Wednesday this year the No. 226 day this month 2nd week this year 33rd week this month 3rd week today's afternoon 161~24 system time 40-11 hour time CST Area

Identifier table:

Symbol meaning Presentation Example
G ERA designator Text AD
Y Year Number 2009
M Month in year Text & Number July & 07
D Day in month Number 10
H Hour in am/pm (1-12) Number 12
H Hour in Day (0-23) Number 0
M Minute in hour Number 30
S Second in minute Number 55
S Millisecond Number 978
E Day in Week Text Tuesday
D Day in year Number 189
F Day's week in month Number 2 (2nd Wed in July)
W Week in year Number 27
W Week in month Number 2
A AM/PM Marker Text Pm
K Hour in Day (1-24) Number 24
K Hour in am/pm (0-11) Number 0
Z Time zone Text Pacific Standard Time
Escape for text Delimiter (none)
Single quote Literal

Text turn: http://blog.csdn.net/yangbobo1992/article/details/9965105

Java uses SimpleDateFormat to format date time. Rp

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.