-1

I am calling a soap webservice and in response I am getting a date, which in the format of "2020-12-31T02:00:00+05:30".

I want to convert it and display it as "31/12/2020, 02:00 AM". How can I do it?

I have tried the below code, but it return's null.

            String pattern = "yyyy-MM-dd'T'HH:mm:ss z";
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            try {
                startDate= format.parse(eventStartDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

Any help would be appreciated. Thanks

  • I believe I closed an identical or nearly identcal question as a duplicate earlier today. Instead of just asking the same question again, please ask a new question spelling out how your question is different from the suggested originals. Then we’re here to help. – Ole V.V. Jan 21 '21 at 10:02
  • I am not getting the answer that I want, please open the question. or please provide the answer of the question. Thanks – Pratik Chauhan Jan 21 '21 at 11:10
  • We’re a lot of people here answering questions to the best of our ability. When we’re not answering yours, it’s probably because you have not explained well enough how it differs from the thousands of similar questions that have already been answered (I don’t think I’m exaggerating). And I don’t find deleting your closed question, reposting it and asking us to do the rest an acceptable behaviour here. It’s also not what’s good for my interest in helping you. – Ole V.V. Jan 21 '21 at 13:34
  • Let me see if I can help you just a little bit anyway, not with your date string conversion, but with your behaviour on Stack Overflow, assuming that you are interested. I suggest that you post a new question linking to the two original questions that I have referred you to and spelling out exactly what you are still missing after reading the 8 answers to those. Also remember to include your attempt and explain in what way it failed. Then we can give you an answer that won’t just repeat those 8 answers that you didn’t want, but one that you will want. – Ole V.V. Jan 21 '21 at 13:42
  • check out the question, do i still need to change it? – Pratik Chauhan Jan 22 '21 at 08:01
  • First off, consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Jan 22 '21 at 09:21
  • Thanks for improving your question. There’s room for still more improvement, but I have reopened and have posted an answer. I am letting my downvote stand for now. – Ole V.V. Jan 22 '21 at 09:25

1 Answers1

1

java.time through desugaring

Consider using java.time, the modern Java date and time API, for your date and time work. The following code will almost give you what you asked for (only the year is only 2 digits and the clock hour only one digit). The advantage is that we’re using built-in formats and need not struggle with any format patterns ourselves — which was the part you had trouble with.

    DateTimeFormatter displayFormatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT);
    
    String fromResponse = "2020-12-31T02:00:00+05:30";
    OffsetDateTime startDateTime = OffsetDateTime.parse(fromResponse);
    String displayDateTimeStr = startDateTime.atZoneSameInstant(ZoneId.systemDefault())
            .format(displayFormatter);
    
    System.out.println(displayDateTimeStr);

I ran the code on my desktop Java 9 using the ThreeTen Backport library (see the link at the bottom) with default locale set to en-IN and default time zone to Asia/Kolkata. Output was:

31/12/20, 2:00 AM

If you insist on four digit year and two digit hour, specify the output format using a pattern:

    DateTimeFormatter displayFormatter = DateTimeFormatter.ofPattern(
            "dd/MM/uuuu, hh:mm a", Locale.forLanguageTag("en-IN"));

31/12/2020, 02:00 AM

I assumed that your user wants to see the time in his or her time zone, so I am performing a conversion using the atZoneSameInstant method of OffsetDateTime.

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 either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

What went wrong in your code?

There are two bugs in your format pattern string:

  1. You have got a space between ss and z. In the string from the response there is no space between the seconds and the time zone offset. This caused your parsing to fail with a java.text.ParseException: Unparseable date: "2020-12-31T02:00:00+05:30". If you didn’t see that, you need to fix your project setup so that output from your e.printStackTrace(); is shown.
  2. Pattern letter z does not accept an offset with a colon between hours and minutes, as you’ve got in +05:30. With the old and troublesome SimpleDateFormat the correct pattern would have been upper case XXX.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    Thank you for the answer. I am fresher and I didn't developed the API. The mistake I was doing was the space between "s" and "z", which you corrected. But After that I again have to convert the GMT format time and get the AM and PM. Thanks a lot you code worked for me. And I will try to understand the information you provided on java.time. – Pratik Chauhan Jan 22 '21 at 09:51