0

I'm using the new Date() function in order to get the current date. However, on the 29th of December, 2019 (29/12/2019), the function begins to display the 29th of December, 2020 (29/12/2020), incorrectly moving the year forward 3 days early.

It then reverts back to the correct year on the 1st of January (01/01/2020), correctly displaying the correct day, month and year.

I have absolutely no idea what is causing this, or why this happens.

Has anyone experienced this before and knows why? Or knows how to fix it?

My code for it is listed below. It's a pretty simple flow of events, but as mentioned above, as soon as the date reaches the 29th of December, the year in the TextView suddenly changes to 2020 instead of 2019. This happens in future years as well (2020 to 2021 etc.)

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/YYYY");

String currentDate = sdf.format(new Date());

startDateTextView.setText(currentDate);

Any help will be greatly appreciated :)

ADM
  • 20,406
  • 11
  • 52
  • 83
Astrazo
  • 5
  • 5

2 Answers2

1

Note: Use modern java.time classes for all your date-time handling code instead of using Calendar and Date.


Coming to your problem

Here in your case, you need to use dd/MM/yyyy instead of dd/MM/YYYY in date formatter. Read Y returns 2012 while y returns 2011 in SimpleDateFormat for more details.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
0

Date c = Calendar.getInstance().getTime(); System.out.println("Current time => " + c);

SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy"); String formattedDate = df.format(c); Please use this to get current date

archana
  • 1
  • 1
  • 1