2

I am not sure whether this has been already answered or not, but can anyone please tell me how can I convert "2019-07-14T18:30:00.000Z" to "2019-07-14 04:30:00 PM" using DateTimeFormatter or any other library in Java? Basically the output date-time should have time in AM/PM format.

Kirti Jha
  • 93
  • 1
  • 3
  • 10
  • what is dependence between these two dates? – Vault23 Sep 26 '19 at 11:38
  • Sorry for typo. I want toconvert "2019-07-14T18:30:00.000Z" to "2019-07-14 04:30:00 PM" using DateTimeFormatter or any other library in Java. The output date time should have time in AM/PM format – Kirti Jha Sep 26 '19 at 11:41
  • On what basis do you have days, month and time difference @KirtiJha – Ryuzaki L Sep 26 '19 at 12:01
  • You can be sure that it has already been asked and answered, or at least that you can put your own answer together from pieces found in existing answers. And yes, you are correct, `DateTimeFormatter` is one of the classes that you will want to use. – Ole V.V. Sep 26 '19 at 13:56
  • 1
    @OleV.V. Looks like it's GMT-02:00, no? [Seems to work](https://ideone.com/U7mvlf). – TiiJ7 Sep 26 '19 at 14:11
  • The last linked question has many answers. Use those that use java.time: [this](https://stackoverflow.com/a/46288403/5772882), [this](https://stackoverflow.com/a/46486064/5772882), [this](https://stackoverflow.com/a/54433865/5772882) and not least [this good one](https://stackoverflow.com/a/57764096/5772882). – Ole V.V. Sep 26 '19 at 14:12
  • Sorry, @TiiJ7, and thanks, how could I miss the *PM*? You are absolutely correct. KirthiJha, the link by TiiJ7 is very helpful, only I’d use a time zone of the form `ZoneId.of("America/Godthab")`. Substitute your desired zone. – Ole V.V. Sep 26 '19 at 14:13

2 Answers2

7

Try this

String date = "2019-07-14T18:30:00.000Z";
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
Date parsedDate = inputFormat.parse(date);
String formattedDate = outputFormat.format(parsedDate);
System.out.println(formattedDate);
Taha
  • 450
  • 4
  • 18
  • 5
    You are ignoring the fact that `Z` designates a particular timezone. You need to call `inputFormat.setTimeZone(TimeZone.getTimeZone("UTC"));` before parsing. – VGR Sep 26 '19 at 13:25
  • 1
    **Please** don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. This was even mentioned in the question, and the question was tagged java.time. You’re suggestion something worse and thus risking leading astray. – Ole V.V. Sep 26 '19 at 13:57
5

By using ZonedDateTime you can parse the input UTC format string and then use LocalDateTime and DateTimeFormatter to format the output string. But i'm not sure on what basis you had days,month and time difference in input and output string

String date = "2019-07-14T18:30:00.000Z";

ZonedDateTime dateTime = ZonedDateTime.parse(date);

String res = dateTime.withZoneSameInstant(ZoneId.of("//desired zone id")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss a"));
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • 1
    The pattern symbol "H" stands for the hour of day. You need "h" instead. Another fault is the missing handling of time zone. Just converting the UTC-Time to "LocalDateTime" is not what the OP wants. – Meno Hochschild Sep 26 '19 at 13:53
  • sorry i missed it, but i updated the answer. And regarding timezone OP never mentioned that he needs output in different timezone and even i quoted that in my answer also @MenoHochschild – Ryuzaki L Sep 26 '19 at 14:42
  • The OP has declared his wish to convert "2019-07-14T18:30:00.000Z" to "2019-07-14 04:30:00 PM". So it is clear that he does not want the UTC-time-of-day (two hours difference in given example). – Meno Hochschild Sep 26 '19 at 15:25
  • is that `GMT-20.00` can you tell me which timezone it is? @MenoHochschild – Ryuzaki L Sep 26 '19 at 15:42
  • Okay i don't know the timezone but i updated the way to do it @MenoHochschild – Ryuzaki L Sep 26 '19 at 15:51
  • I don't know which zone the OP really uses (the offset might be UTC-02). Anyway, using `toLocalDateTime()` is a problem. Better use the `ZonedDateTime`-method `withZoneSameInstant(ZoneId zone)`. – Meno Hochschild Sep 26 '19 at 16:25
  • 1
    Okay, I have now removed my downvote because of your correction. – Meno Hochschild Sep 27 '19 at 04:31