0

In my app, I noticed that the day from the datetime is wrong

By using:

DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
final Calendar cal = Calendar.getInstance();
date = dateFormat.format(cal.getTime());

It gives me 2021-02-49 09:41:29, as you can see. the day from the datetime is 49. Why is it like that?

yareyaredaze
  • 85
  • 2
  • 10
  • 1
    `DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");` - `YYYY` is a week-based-year. – Elliott Frisch May 18 '21 at 02:12
  • Since today is May 17 (**Month=5**, Day=17), it is weird that it comes up with **Month=2**, Day=49. I wonder what kind of non-Gregorian calendar it might be using to come up with values like that. Try printing the `Calendar` object to see if it says which kind of calendar is used. – Andreas May 18 '21 at 03:03
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. May 18 '21 at 03:21
  • I cannot reproduce either (just got `2021-05-18 04:22:45`). – Ole V.V. May 18 '21 at 03:23

0 Answers0