java.util.Date is generating a wrong date?

前端 未结 3 1521
时光取名叫无心
时光取名叫无心 2020-12-06 23:47

Here\'s my code:

java.util.Date TODAY = new java.util.Date();     
SimpleDateFormat SDF = new SimpleDateFormat( \"YYYY-MM-DD\" );
System.out.println ( SDF.fo         


        
3条回答
  •  隐瞒了意图╮
    2020-12-07 00:45

    When format for SimpleDateFormat is specified as follows:

    SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
    
    • the YYYY - means week year,
    • the MM - means month,
    • the DD - means day in year.

    Week year here is not what you wanted. See what is week year.

    Your today's date is 2015-02-02, which means that it is 32 days since the beginning of the year 2015 passed and your are on the 33 day. That is why you get date "2015-02-33".

    To mean year (and not week year) and day in month change format to SimpleDateFormat("yyyy-MM-dd");

提交回复
热议问题