1

I am trying to convert date from (ddmmyyyy HH:mm:ss) to (yyyy-MM-dd HH:mm:ss.SSS) format.

Below is the code :

String startDate="06162019 00:00:00";
SimpleDateFormat inSDF = new SimpleDateFormat("ddmmyyyy HH:mm:ss");
SimpleDateFormat outSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
try{
   String outDate = "";
   Date date = inSDF.parse(startDate);
   System.out.println(date);
   outDate = outSDF.format(date);
   System.out.println(outDate);
}catch (final Exception e) {
   e.getMessage();
}

But i am getting wrong result :

Sun Jan 06 00:00:00 GMT 2019
2019-01-06 00:00:00.000

Any help would be appreciated?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Raj Raichand
  • 97
  • 2
  • 9
  • 4
    Your first pattern uses `mm` in the middle of (presumbly) the year. `mm` is minutes, not months. Change first pattern to `ddMMyyyy HH:mm:ss` – Michael Jul 30 '19 at 10:56
  • 5
    Moreover, if the format is `ddMM`, and your example date starts with `0616`, that is the 6th day of the 16th month. There is no 16th month in the common calendar. – RealSkeptic Jul 30 '19 at 10:57
  • @RealSkeptic that's what i was wondering – Pranali Rasal Jul 30 '19 at 11:41
  • 1
    Don't use `SimpleDateFormat`. That class is notoriously troublesome. Your experience is not the only one, rather it is pretty common. The class is also long outdated. Instrad use `LocalDateTime` and `DateTimeFormatter`. [Oracle tutorial here](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. Jul 30 '19 at 13:44

2 Answers2

4

I assume that you work with Java 8 or later. If so please drop old and horrible java.util.Date and and even worse java.text.SimpleDateFormat they are dead and buried. Switch to use of java.time package. in order to solve your problem you would need to do this:

String startDate="06162019 00:00:00";
DateTimeFormatter inSDF = DateTimeFormatter.ofPattern("MMddyyyy HH:mm:ss");
DateTimeFormatter outSDF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
try{
   System.out.println(outSDF.format(inSDF.parse(startDate)));
}catch (Exception e) {
   e.getMessage();
}

See this code run live at IdeOne.com.

2019-06-16 00:00:00.000

Read about DateTimeFormatter. Also you might find this article interesting: Java 8 java.time package: parsing any string to date

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
1

A lower-case m represent minutes, while an upper-case m represents months.

You need to change SimpleDateFormat inSDF = new SimpleDateFormat("ddmmyyyy HH:mm:ss"); to SimpleDateFormat inSDF = new SimpleDateFormat("ddMMyyyy HH:mm:ss");

  • ... and use that format for `startDate`: `String startDate="16062019 00:00:00";` – Maurice Perry Jul 30 '19 at 11:08
  • Used with the string from the question I expect 2020-04-06 00:00:00.000. Which I don't think was the expected. It's not you. It's `SimpleDateFormat` being harder to use correctly than you thought. – Ole V.V. Jul 30 '19 at 13:58
  • 1
    FYI, the terribly troublesome date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/10/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/10/docs/api/java/util/Calendar.html), and `java.text.SimpleDateFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [*java.time*](https://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes built into Java 8 and later. See [*Tutorial* by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Jul 30 '19 at 20:23