Mosam Mehta
Mosam Mehta

Reputation: 1668

Java shows incorrect current year

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?

Upvotes: 4

Views: 192

Answers (1)

Maroun
Maroun

Reputation: 95958

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

Upvotes: 11

Related Questions