1

I use the code below to change the date format of the date. But the result changes to different date, it becomes 12/27/2021. When I checked it, it says it uses Gregorian Calendar. Anyone know how to fix this.

        String date = "23-03-2021";
        Date d = new SimpleDateFormat("dd-MM-YYYY").parse(date);
        SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/YYYY");
        String paymentdate = outputFormat.format(d);
        System.out.println(paymentdate);

Actual:

12/27/2021

Expected:

03/23/2021
Anish B.
  • 9,111
  • 3
  • 21
  • 41
jajaja
  • 389
  • 1
  • 12
  • 26
  • 2
    That happens because you are using upper-case `YYYY` for the year, instead of lower-case `yyyy`. Upper-case `Y` means "week year", which is not exactly the same as calendar year. See the [API docs of `SimpleDateFormat`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html). – Jesper Apr 20 '21 at 09:09
  • 4
    If at all possible you should stop using `Date` which is poorly designed and use the java.time classes such as `LocalDate` and `DateTimeFormatter` – greg-449 Apr 20 '21 at 09:21
  • @Jesper thanks! it solved my problem. Should I use also lowercase MM? – jajaja Apr 20 '21 at 09:31
  • 2
    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 `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 20 '21 at 10:06
  • 1
    No, do not use lower-case `mm`, because that means minutes instead of months. It's all in the [API docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html) that I mentioned. – Jesper Apr 20 '21 at 11:36

2 Answers2

1

The problem is that you're using YYYY instead of yyyy. I tried your code snippet and that should solve your problem.

Flavius D.
  • 53
  • 4
1

Use yyyy instead of YYYY because Y represents the week of year or week year whereas y represents only year.

According to Java Docs on Week Of Year and Week Year :

A week year is in sync with a WEEK_OF_YEAR cycle. All weeks between the first and last weeks (inclusive) have the same week year value. Therefore, the first and last days of a week year may have different calendar year values.

Modified code :

String date = "23-03-2021";
Date d = new SimpleDateFormat("dd-MM-yyyy").parse(date);
SimpleDateFormat outputFormat = new SimpleDateFormat("MM/dd/yyyy");
String paymentdate = outputFormat.format(d);
System.out.println(paymentdate);

Output :

03/23/2021

Don't use java.util.Date as it is deprecated and it will be removed in the future. Better use classes from java.time package.

Equivalent code using java.time classes :

String date = "23-03-2021";
TemporalAccessor d = DateTimeFormatter.ofPattern("dd-MM-yyyy").parse(date);
String paymentdate = DateTimeFormatter.ofPattern("MM/dd/yyyy").format(d);
System.out.println(paymentdate);

Output :

03/23/2021
Anish B.
  • 9,111
  • 3
  • 21
  • 41