Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java shows incorrect current year

Tags:

java

datetime

I have written code for printing current date.

Date date = new Date();
DateFormat dateFormat = new SimpleDateFormat("ddMMYYYY");
String newdate = dateFormat.format(date);

But it gives output as : 29122016
Actual output should be : 29122015
The system date is also correct.

What is wrong with the code?

like image 217
Mosam Mehta Avatar asked Dec 19 '22 21:12

Mosam Mehta


1 Answers

Y denotes a week year. You should change to:

DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");

The SimpleDateFormat has the details:

Letter | Date or Time Component | Presentation | Examples
-------+------------------------+--------------+----------
y      | Year                   | Year         | 1996; 96
Y      | Week year              | Year         | 2009; 09
like image 160
Maroun Avatar answered Dec 21 '22 10:12

Maroun