-4

The date receiving from server is not fixed in specific format then how can I detect which format am getting so that I can convert it into specific date format and display in the app according to the functionality.

Here are some examples that am receiving are:

2018-09-10T10:35:00.377Z
2018-09-10T10:35:00.12Z
2018-09-10

Thanks in advance!

Nassa44
  • 308
  • 2
  • 4
  • 15
  • 1
    how do you want to display output?? – Ramees Thattarath Sep 10 '18 at 09:39
  • Actually, I have to display in 3-4 options on different screens according to functionality and that's not a big deal but the main thing is to detect the input format – Nassa44 Sep 10 '18 at 09:41
  • "yyyy-MM-dd'T'HH:mm:ss.SSSZ" Read string and substring and check if matches with pattern. Try to define all the patterns which you may receive from server. – Ankita-AR Sep 10 '18 at 09:45
  • Thanks @Ankita-user3449434, I already tried this one and its only working when the input date is in "yyyy-MM-dd'T'HH:mm:ss.SSSZ" format and app crashed when format is different like "yyyy-MM-dd'T'HH:mm:ssZ" – Nassa44 Sep 10 '18 at 09:52
  • Like, I want to know about any default method to check the input date formatter? – Nassa44 Sep 10 '18 at 09:54
  • Possible near-duplicate of [DateTimeFormatter Accepting Multiple Dates and Converting to One (java.time library)](https://stackoverflow.com/questions/44600420/datetimeformatter-accepting-multiple-dates-and-converting-to-one-java-time-libr) and other questions. – Ole V.V. Sep 10 '18 at 10:11
  • Your formats are allowed variants of [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601). java.time, the modern Java date and time API, parses these formats without any explicit formatter. If your string contains a `T`, use the one-arg `OffsetDateTime.parse()`, if not, the one-arg `LocalDate.parse()`. – Ole V.V. Sep 10 '18 at 10:14
  • When you say “date”, does that mean that you don’t need the time portion that is in some of your example strings? – Ole V.V. Sep 10 '18 at 10:28
  • @OleV.V. I think I am not sure about the possibilities, so I am deleting my answer to not confuse the PO. – Khemraj Sharma Sep 10 '18 at 10:43
  • @OleV.V. Yes I need the time portion for some sorting feature. – Nassa44 Sep 10 '18 at 11:21

1 Answers1

2

java.time

    String[] exampleStrings = {
            "2018-09-10T10:35:00.377Z",
            "2018-09-10T10:35:00.12Z",
            "2018-09-10"
    };

    for (String example : exampleStrings) {
        if (example.contains("T")) {
            OffsetDateTime dateTime = OffsetDateTime.parse(example);
            System.out.println("Date: " + dateTime.toLocalDate() 
                    + " Time: " + dateTime.toLocalTime() 
                    + " Offset: " + dateTime.getOffset());
        } else {
            LocalDate date = LocalDate.parse(example);
            System.out.println("Date: " + date);
        }
    }

Your formats are allowed variants of ISO 8601. java.time, the modern Java date and time API, parses these formats without any explicit formatter. The first two example strings have date and time and the characteristic T to separate them, and an offset from UTC (Z means offset 0). The third has only a date. So test whether the string has the T in it and use the corresponding java.time class for the rest.

Output from the above snippet is:

Date: 2018-09-10 Time: 10:35:00.377 Offset: Z
Date: 2018-09-10 Time: 10:35:00.120 Offset: Z
Date: 2018-09-10

I don’t know how you will handle the two different types OffsetDateTime and LocalDate in the rest of your code. If you don’t need the time part, just the date, use dateTime.toLocalDate() to get a LocalDate and just pass this type on in all cases. Since you do need the time part, you may, depending on your situation and requirements, get away with the opposite conversion: in the date case you may get an OffsetDateTime from for example date.atStartOfDay().atOffset(ZoneOffset.UTC), but please do check whether this gives you an appropriate time.

I do agree with the now deleted answer by Khemraj, though, that your server ought to be able to deliver one consistent format or at least indicate which format it is giving you.

Question: Can I use java.time on Android?

Yes, java.time works nicely on 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, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new 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