1

I am trying to parse a DateTime from C# to Java. Here is what comes as string from the java call "2018-08-02T14:24:40.040353". When I try to parse it, I get the following error Unparseable date: "2018-08-02T14:24:40.040353"

Here is the method for parsing the date

public static String dateFormater(String dateFromJSON, String 
    expectedFormat, String oldFormat) {
    SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
    Date date = null;
    String convertedDate = null;
    try {
        date = dateFormat.parse(dateFromJSON);
        SimpleDateFormat simpleDateFormat = new 
        SimpleDateFormat(expectedFormat);
        convertedDate = simpleDateFormat.format(date);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return convertedDate;
}
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Kristiyan Gergov
  • 161
  • 1
  • 11
  • 2
    What are the parameters you're using in the function call? oldFormat should look something like "yyyy-MM-dd'T'HH:mm:ssZ" – Eric Yang Aug 07 '18 at 12:36
  • 1
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Aug 07 '18 at 12:40
  • 2
    You are missing the most important information - the precise value of `oldFormat` – Gyro Gearless Aug 07 '18 at 12:53
  • 1
    FYI: You are using terrible old date-time classes that were supplanted years ago by the *java.time* classes. – Basil Bourque Aug 07 '18 at 15:37

1 Answers1

6

Your date look like a ISO_LOCAL_DATE_TIME, If you are using Java8 you can use java.time API and use the default format of LocalDateTime like this :

String dateString = "2018-08-02T14:24:40.040353";
LocalDateTime ldt = LocalDateTime.parse(dateString);

ldt.toString():

2018-08-02T14:24:40.040353

Community
  • 1
  • 1
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140