YYYY difference SimpleDateFormat yyyy and in the JAVA

Recently, a function is to enter a date format, the example given is designed YYYY-MM-dd HH: mm: ss, so I simply validate what is possible, and so wrote in the manual. Then accidentally found places with yyyy ah, ah difference in the end God horse, or just use it. Looked under jdk, say so:

 

That Y represents the Week year, however, this Week year and what it is. . jdk document caring gives Examples, but did not use any eggs. .

After testing, the results are as follows: Week year mean is the year where the day belongs to week, starting from Sunday, Saturday end of the week, as long as this week, New Year's Eve, this week into next year even if, for example, 2010.12.26 calendar long like this:

 

12.26 where the last day of the week is 2011.1.1, this day belongs to 2011, so the 2010.12.26 of the Week year is 2011, with the format YYYY do, you'll get 2011.12.26.

Test Code:

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    // 2014-12-26
    calendar.set(2010, Calendar.DECEMBER, 26);
    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));
}

Operating results:
the Result for YYYY: 2011-12-26
the Result for yyyy: 2010-12-26
----------------
Disclaimer: This article is CSDN bloggers' overtime work until dawn "the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/bewilderment/article/details/48391717

Guess you like

Origin www.cnblogs.com/kakaisgood/p/11571618.html