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;
}
YCF_L
  • 51,266
  • 13
  • 85
  • 129
Kristiyan Gergov
  • 161
  • 1
  • 10
  • 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
YCF_L
  • 51,266
  • 13
  • 85
  • 129
  • Even if you are using Java 6, you can use a backport of JSR-310. +1 – Peter Lawrey Aug 07 '18 at 12:58
  • Thank you @PeterLawrey new thing for me – YCF_L Aug 07 '18 at 13:00
  • 1
    Yes @PeterLawrey I use this library in my old projects, it is really helpful – YCF_L Aug 07 '18 at 13:03
  • Does it have any alternative because i am using it in android and the Call requires API level 26 – Kristiyan Gergov Aug 08 '18 at 05:20
  • 1
    @KristiyanGergov yes al already mentioned by PeterLawrey you can use https://github.com/JakeWharton/ThreeTenABP library in your project, it has almost every thing from java.time – YCF_L Aug 08 '18 at 05:37
  • 1
    More helpful details in [this question: How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Aug 08 '18 at 08:35
  • 1
    Thank you @OleV.V. this should be the answer of that OP's comment – YCF_L Aug 08 '18 at 08:39
  • 1
    @KristiyanGergov take a look at the link that Ole V.V. share it can gives you a good details [How to use ThreeTenABP in Android Project.](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project) – YCF_L Aug 08 '18 at 08:40