0

Hi i have a strange problem. I am fetching some xml data from the web which is working as expected. When i call the method which fetches the data in on of my fragments everything is fine but if i call exactly the same method for the same url in another fragment it throws this weird parsing exception where i am trying to convert the incoming date string into a long timestamp in milliseconds. If the error occurred once in the fragment where it isn't working, it also stops working at the fragment where it previously worked. it throws this exception:

java.text.ParseException: Unparseable date: "Sun, 30 Aug 2020 00:32:42 +0000"

and the pattern i am using for simple date format is

"EEE, dd MMM yyyy HH:mm:ss Z"

can anybody tell me why this is happening because the pattern i'm using seems to work so why do i get this exception?

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
Martin S
  • 363
  • 3
  • 14
  • The related question doesn't really fits to my so why has my been associated with it? That's all about NumberFormatException caused by threading and that's not happening in my application. – Martin S Aug 31 '20 at 18:42
  • Just as i thought my problem had nothing to do with the question why my was marked as duplicate! The problem was that i was using Locale.getDefault() as second parameter of SimpleDateFormat and the date string i was trying to parse was localized in english. So if the local from Locale.getDefault() doesn't return english, it always threw this exception because the englisch localized date string (month name) was unknown by the local returned. So in my case the answer was just to set Locale.ENGLISH as second param of SimpleDateFormat because the dates i am receiving are always transl. in english. – Martin S Sep 01 '20 at 21:55
  • Thanks for reporting back, Martin S. If you had posted your code in the question from the outset, we would have been able to spot that that was the problem and could have helped you correctly at once. Your observation doesn’t seem to explain why it works in one fragment and not in the other one, though. In any case I have changed the link to refer to a more appropriate original question. – Ole V.V. Sep 02 '20 at 02:47
  • 1
    My fault, the problem with why it worked in one fragment and in the other not had to do with the way I am deploying my app with android studio when testing. I've selected that the app should be deployed as APK from bundle because I am using Dynamic feature delivery and strangely the local is switching from English to German when starting new activity where the other fragment resides in. This doesn't happen when I deploy the app to the PlayStore, only on debug build but fixed it as I mentioned by setting the Local always to English and not using Locale.getDefault(). Next time I be more precise. – Martin S Sep 02 '20 at 03:10
  • Yes the new one is exactly what I was looking for but I think because of the Title of the Question, I was unable to find it. Thanks again – Martin S Sep 02 '20 at 03:14

1 Answers1

1

I recommend you switch from the outdated and error-prone java.util date-time API and SimpleDateFormat to the modern java.time date-time API and the corresponding formatting API (package, java.time.format). Learn more about the modern date-time API from Trail: Date Time.

If your Android API level is still not compliant with Java8, check How to use ThreeTenABP in Android Project and Java 8+ APIs available through desugaring.

Do it as follows using the Java modern date-time API:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        // Given date-time string
        String dateTimeString = "Sun, 30 Aug 2020 00:32:42 +0000";

        // Parse the given date-time string into OffsetDateTime as it has a Zone Offset
        // of +0000 hours
        OffsetDateTime odt = OffsetDateTime.parse(dateTimeString, DateTimeFormatter.RFC_1123_DATE_TIME);

        // Display OffsetDateTime in the default format
        System.out.println(odt);

        // Display OffsetDateTime in some custom formats
        System.out.println(odt.format(DateTimeFormatter.RFC_1123_DATE_TIME));
        System.out.println(odt.format(DateTimeFormatter.ofPattern("u-MM-dd HH:mm:ss X")));
    }
}

Output:

2020-08-30T00:32:42Z
Sun, 30 Aug 2020 00:32:42 GMT
2020-08-30 00:32:42 Z
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • My minimum API is 23 and I don't want to add a third party dependency only for this because it seems very inefficient. – Martin S Aug 31 '20 at 18:45
  • Martin, did you consider [desugaring](https://developer.android.com/studio/write/java8-support-table)? Not that there’s anything inefficient about ThreeTenABP. – Ole V.V. Sep 02 '20 at 02:49