0

I'm trying convert datetime from format "dd/MM/yyyy HH:mm:ss" to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'". It work well, but sometime still error. Example: from "14/07/2020 17:46:32" -> convert result "2188-04-14T17:32:32.000Z", and i don't know why ? Can someone please explain to me. Thanks.

prayagupa
  • 30,204
  • 14
  • 155
  • 192
Vinh Luffy
  • 19
  • 3
  • Your question is incomplete for stackoverflow. Can you please put how your code looks like only then people can help. – prayagupa Jul 16 '20 at 04:31
  • Sorry not able to understand properly. What you do mean by sometimes? – Harmandeep Singh Kalsi Jul 16 '20 at 04:40
  • 1
    That sounds like `SimpleDateFormat`! A notorious troublemaker of a class. And long outdated. I recommend you don’t use it. Instead use `DateTimeFormatter`, `ZoneOffset` and `OffsetDateTime`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 16 '20 at 05:18
  • I am sure we can help! When you show us your faulty code and tell us the expected result. – Ole V.V. Jul 16 '20 at 05:19
  • Not sure it’s exactly what you want: `LocalDateTime.parse("14/07/2020 17:46:32", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss")).atOffset(ZoneOffset.UTC).toString()` yields `2020-07-14T17:46:32Z`. – Ole V.V. Jul 16 '20 at 05:23
  • This is my code: private static String getTransTime() { String time1 = "14/07/2020 17:46:32"; Calendar calendar = Calendar.getInstance(); calendar.setTime(df.parse(time1)); SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); String time2= fmt.format(calendar.getTime()); return time2; } – Vinh Luffy Jul 16 '20 at 06:07
  • I have singleton Class process http request, in this class has method getTransTime(). Class process and get time correct in almost request, sometimes incorrect. ex: "14/07/2020 17:46:32" -> "2188-04-14T17:32:32.000Z". – Vinh Luffy Jul 16 '20 at 06:13

0 Answers0