Java date() giving the error date

See English answer > java parsing string to date 1

It is obvious that there are not 43 days in February. I don't know why it returned to this I've looked everywhere for solutions, but they don't seem to fit the specific problems I encounter This is the code:

public String getDate(){
    DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD");
    Date date = new Date();

    return dateFormat.format(date);
}

Just for the record, I tried to use calendar GetInstance () and so on have the same result Interestingly, when I try

get(Calendar.DAY_OF_MONTH)

It returns 12, so the number is correct in some places, but there is a problem between the two

thank you

Solution

DD stands for date and DD stands for date You want yyyy MM DD (case sensitive)

public String getDate(){
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date date = new Date();

    return dateFormat.format(date);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>