The difference between SimpleDateFormat yyyy and YYYY in JAVA __java

Source: Internet
Author: User

A recent feature is the format of the input date, the design given the example is Yyyy-mm-dd HH:mm:ss, so I simply verified that it is OK, and then in the manual. Then accidentally found that there are places to use yyyy ah, this is the difference between God and horse, or casually use it. Look at the JDK, so to speak:


That means Y is week year, but what is this week year? The JDK documentation is examples, but there is no egg to use.

After testing, the results are as follows: Week year means that the week in which the day is located, the week from Sunday onwards, the end of Saturday, as long as this week, this week even in the next year, such as 2010.12.26, the calendar grew up like this:


The last day of the week of 12.26 is 2011.1.1, this day belongs to 2011, so the 2010.12.26 week year is 2011, use yyyy do format, will get 2011.12.26.

Test code:

public static void Main (string[] args) {
Calendar calendar = calendar.getinstance ();
2014-12-26
calendar.set (calendar.december, num);
Date strDate1 = Calendar.gettime ();
SimpleDateFormat f1 = new SimpleDateFormat ("Yyyy-mm-dd");
SYSTEM.OUT.PRINTLN ("Result for YYYY:" + F1.format (strDate1));
SimpleDateFormat F2 = new SimpleDateFormat ("Yyyy-mm-dd");
SYSTEM.OUT.PRINTLN ("Result for yyyy:" + f2.format (strDate1));
}

Run Result:

Result for yyyy:2011-12-26
Result for yyyy:2010-12-26

This result is in the JDK 1.7 version of the test results, the remaining version of what the results are not clear.

Java version "1.7.0_75"
Java (tm) SE Runtime Environment (build 1.7.0_75-b13)
Java HotSpot (tm) 64-bit Server VM ( Build 24.75-b04, Mixed mode)

Some articles say that this week's Thursday is the year, which is the year, but it is not the case, ah, 2010.12.26 Thursday in 2010, but the results are still 2011 years, so I have to think that the principle is as long as the span of the year belongs to the next year.
In short, just want to format our ordinary people know the date, with " yyyy-mm-dd HH:mm:ss" is enough. yyyy or something, it's okay to stop using it.


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.