Skip to content
Advertisement

Android date is incorrect

I am using Calendar class to get year, month and day:

JavaScript

Today is 11 Dec 2021 (2021-12-11). But toast alert shows 2021-11-11. Also I tried to set calendar time to a new Date class but still wrong

Advertisement

Answer

tl;dr

I am using Calendar class

Don’t.

Use only java.time classes.

JavaScript

2021

Details

The Answer by Modi is correct.

Furthermore, never use Calendar. That terrible class was built by people who did not understand date-time handling. Along with Date and SimpleDateFormat, these classes were years ago supplanted by the modern java.time classes.

Specify a time zone to determine date. For any given moment the date varies around the globe by time zone. May be tomorrow in Japan 🇯🇵 while simultaneously yesterday in Canada 🇨🇦.

JavaScript

Interrogate for parts.

Unlike the legacy classes, the java.time classes use sane numbering. Year 2021 is 2021, and so on. Months are 1-12 for January through December.

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement