0

I'm trying to use Joda-Time library to convert a String date and time to Date but the result I get is not the expected.

From the server I get:

08/11/2017 12:30
10/11/2017 12:30

Joda converts it to:

2017-01-08T12:30:00.000+02:00
2017-01-10T12:30:00.000+02:00

DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/mm/yyyy HH:mm:ss");

// add two :00 at the end for the seconds
startDate = startDate +":00";
DateTime start = formatter.parseDateTime(startDate);
System.out.println(start.toString());

endDate= endDate + ":00";
DateTime end = formatter.parseDateTime(endDate);
enkara
  • 6,189
  • 6
  • 34
  • 52
chris_cs
  • 35
  • 6
  • 08/11/2017 12:30 === > 2017-01-08T12:30:00.000+02:00 and 10/11/2017 12:30 === > 2017-01-10T12:30:00.000+02:00 – chris_cs Nov 08 '17 at 12:52
  • 1
    The pattern "dd/mm/yyyy HH:mm:ss" is surely not okay because the first occurrence of "mm" should rather be "MM" (month, not minute). – Meno Hochschild Nov 08 '17 at 13:22

1 Answers1

1

That's because you're using mm for the month, but the correct pattern is uppercase MM. Check the documentation for more details.

One more thing. If your input doesn't have the seconds (:00), you don't need to append it in the end of the input strings. You can simply create a pattern without it:

// "MM" for month, and don't use "ss" for seconds if input doesn't have it
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm");

// parse input (without ":00" for the seconds)
DateTime start = formatter.parseDateTime("08/11/2017 12:30");
System.out.println(start.toString());

The output will be:

2017-11-08T12:30:00.000-02:00

Notice that the offset (-02:00) is different from yours. That's because DateTime uses the default timezone if you don't specify one.

joder
  • 26
  • 1