2

Following is the code to parse date. I have used 'joda-time:joda-time:2.9.9' lib for formatter.

String date = "Sun Sep 04 17:29:52 +0000 2022";
DateTimeFormatter dateFormat = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss Z 
yyyy").withLocale(Locale.UK);
dateFormat.parseDateTime(date);

Above code was throwing illegelArgument exception in Android 12. When I changed locale from UK to US, its started working.

But strange thing is that if I tried to parse Wed Mar 23 14:28:32 +0000 2016 this date with above code, its working in all OS.

Out of mind question is why one date is getting parse and another is not.

What's actually changed in Android 12 that suddenly code is getting failed?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Rohan Patel
  • 1,758
  • 2
  • 21
  • 38

2 Answers2

1

What's actually changed in Android 12 that suddenly code is getting failed?

Earlier, the short name for September in the Locale.UK was Sep but it got changed to Sept starting with Java 16. Check this related thread.

There is no change in other short names in the Locale.UK and therefore it worked for Wed Mar 23 14:28:32 +0000 2016 for example.

Modern Date-Time API

For the sake of completeness, I would like to discuss a bit about the modern date-time API. Probably you must have already seen the following note on the Home Page of the Joda-Time API:

Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.

Demo using the modern date-time API:

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

public class Main {
    public static void main(String[] args) {
        String strDateTime = "Sun Sep 04 17:29:52 +0000 2022";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss Z yyyy", Locale.ROOT);
        OffsetDateTime zdt = OffsetDateTime.parse(strDateTime, formatter);
        System.out.println(zdt);
    }
}

Output:

2022-09-04T17:29:52Z

Notice that I have use Locale.ROOT in the demo. If you use Locale.UK, it will throw the same error that you have got. However, if you change Sep to Sept and use Locale.UK, it will work.

Learn more about the modern Date-Time API from Trail: Date Time.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
-1
import java.text.SimpleDateFormat;
import java.util.Locale;

Locale locale = new Locale("bd", "bn");
String pattern = "EEEEE MMMMM yyyy HH:mm:ss.SSSZ";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale);
String date = simpleDateFormat.format(System.currentTimeMillis());
System.out.println("DateTime :: " + date);
  • Can you please tell me why above code is not working Android 12? – Rohan Patel Sep 06 '22 at 06:20
  • https://www.joda.org/joda-time/changes-report.html#a2.11.1 update the library with lasted version. see the change logs. answer: when I run your given code it gives an error which is a format exception, your date & time is not correct. – S.m. Kamal Hussain Shahi Sep 06 '22 at 08:58