-2

I have an UTC datetime in the format 2019-12-06T06:04:50.022461Z I want to convert into readable datetime in the format dd-mm-yyyy hh:mm:ss. I am not able to convert in the particular format. Any help will be highly appreciated. Thanks

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Piyush
  • 492
  • 5
  • 22
  • Does this answer your question? [How to get start and end range from list of timestamps?](https://stackoverflow.com/questions/43709133/how-to-get-start-and-end-range-from-list-of-timestamps) There are thousands of questions out there (literally) that seem related to yours, so please go through at least the first 20 of those before asking a new question. – Ole V.V. Dec 07 '19 at 11:43
  • Please tell us what your search and research turned up and show us how your attempt failed, for example what other format you got instead. [I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. Dec 07 '19 at 11:45
  • 1
    I think there are two non-trivial issues with the task you are trying: (1) How to parse an ISO 8601 string with trailing `z` (your string is in ISO 8601 format); [there’s a good answer by Andreas here](https://stackoverflow.com/a/52671099/5772882). (2) How to parse a string with 6 decimals on the seconds because there is no way that `SimpleDateFormat` can do that. There are answers [here](https://stackoverflow.com/questions/58705924/timestamp-convert) and [my own answer here](https://stackoverflow.com/a/57800178/5772882). – Ole V.V. Dec 07 '19 at 15:01

3 Answers3

2

java.time and ThreeTenABP

I am assuming that you want to display the time in your user’s time zone. As you said already, your string is in UTC, and very few users will be happy to read the time in UTC.

    DateTimeFormatter readableFormatter = DateTimeFormatter.ofPattern("dd-MM-uuuu HH:mm:ss");

    String originalString = "2019-12-06T06:04:50.022461Z";
    String readableString = Instant.parse(originalString)
            .atZone(ZoneId.systemDefault())
            .format(readableFormatter);
    System.out.println(readableString);

For the sake of the example I ran this code in America/Sitka time zone. Output was:

05-12-2019 21:04:50

If you did want to display the time in UTC as in the original string:

    String readableString = OffsetDateTime.parse(originalString)
            .format(readableFormatter);

06-12-2019 06:04:50

Don’t go with the classes SimpleDateFormat, TimeZone and Calendar used in the currently accepted answer. They are all poorly designed, SimpleDateFormat in particular is a notorious troublemaker and also cannot parse 6 decimals on the seconds as in your string. Those three classes are also all long outdated. Instead I am using java.time, the modern Java date and time API. It is so much nicer to work with. The code is not only shorter, I think that you will also find it clearer to read (either immediately or when you get used to the fluent style).

I am exploiting the fact that your original string is in ISO 8601 format. The classes of java.time parse ISO 8601 format as their default, that is, without any explicit formatter.

For the vast majority of purposes you shold not want to convert from one string format to another, though. Inside your program keep your date and time as an Instant or other proper date/time object. only convert to a string when a string is needed in the interface.

Question: Doesn’t java.time require Android API level 26?

java.time works nicely on both older and newer Android devices. It just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
1

You can use SimpleDateFormat to format the date. Check below:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

Calendar calendar = Calendar.getInstance();

try {
    calendar.setTime(sdf.parse("2019-12-06T06:04:50.022461Z"));
} catch (Exception ex) {
    ex.printStackTrace();
}

SimpleDateFormat returnFormat = new SimpleDateFormat("dd-mm-yyyy hh:mm:ss");
returnFormat.format(calendar.getTime());
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46
  • 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`. Yes, you can use it on Android. For older Android see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Dec 07 '19 at 11:40
  • Your code yields `06-04-2019 07:04:50` in my time zone. The month should be December but has been changed to April. – Ole V.V. Dec 09 '19 at 04:04
0

You can try this in java 8

    String dateString = "2019-12-06T06:04:50.022461Z";
    DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC);
    TemporalAccessor date = fmt.parse(dateString);
    Instant time = Instant.from(date);

    DateTimeFormatter fmtOut = DateTimeFormatter.ofPattern("dd-mm-yyyy hh:mm:ss").withZone(ZoneOffset.UTC);
    String newDateString = fmtOut.format(time);
  • Your code gives me `java.time.format.DateTimeParseException: Text '2019-12-06T06:04:50.022461Z' could not be parsed, unparsed text found at index 10`. Using java.time, the modern Java date and time API, is a good idea. – Ole V.V. Dec 07 '19 at 11:41
  • @OleV.V. Why dont you give the sample code to parse it. Thanks – Piyush Dec 09 '19 at 03:49
  • @Piyush I have been hesitating to do that both because it’s a poor question and it’s a duplicate of many others. So I provided some links in the comments at first. Now I have followed your request and posted my answer. – Ole V.V. Dec 09 '19 at 04:09
  • @OleV.V. Thank you for the information!! and your answer – Piyush Dec 09 '19 at 04:11