0
votes

Not able to parse String date Sun Dec 06 11:15:00 IST 2015 using SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy") when I use Locale.ENGLISH in SimpleDateFormat without Locale.ENGLISH working but

Not able parse when app language changes to non-English language like Marathi/Hindi.

I am able to parce other string date like 10 12 2018 to dd MM yyyy but not above format.

Facing issue on Lollipop and kit-kat devices. when I change Locale to non-English language.

minSdkVersion 16 and targetSdkVersion 27

1.Tried following solutions.

EEE MMM dd HH:mm:ss Z yyyy ,EEE MMM dd HH:mm:ss z yyyy ,DateFormat etc

String tDueDate="Sun Dec 06 11:15:00 IST 2015"; 
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
        Date dt = sdf.parse(tDueDate);
        sdf = new SimpleDateFormat("dd MMM yyyy", Locale.ENGLISH);
        return sdf.format(dt);

Sat Oct 03 00:00:00 IST 2018 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: java.text.ParseException: Unparseable date: "Sat Oct 03 00:00:00 IST 2018" (at offset 20) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at java.text.DateFormat.parse(DateFormat.java:555) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at com.smartsms.util.EventsUtil.returnDateInDateFormat(EventsUtil.java:750) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at com.smartsms.fragment.EventsFragmentNew$MyTask.onPostExecute(EventsFragmentNew.java:1397) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at com.smartsms.fragment.EventsFragmentNew$MyTask.onPostExecute(EventsFragmentNew.java:1295) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:632) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at android.os.Looper.loop(Looper.java:136) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5111) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at java.lang.reflect.Method.invoke(Method.java:515) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:806) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events W/System.err: at dalvik.system.NativeStart.main(Native Method) 12-12 13:16:33.733 17653-17653/com.smartsms.organizer.events

Activitylocale: en_US

4
Which version of SimpleDateFormat are you using java.text.SimpleDateFormat or android.icu.text.SimpleDateFormat?Morrison Chang
@MorrisonChang java.text.SimpleDateFormat; format I am targeting minimum of version 16Sagar
Have you tried Locale.US instead. Suggested by the Android documentation on Locale - Be wary of the default localeMorrison Chang
@MorrisonChang yes I have tried Having issue on kitkat and lollipop rest its workingSagar
It looks like you are trying to parse a string from Date.toString? Could you just avoid the toString call and instead get hold of the original Date object?Ole V.V.

4 Answers

0
votes

try with change code like

 SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.getDefault());
        Date dt = sdf.parse(tDueDate);
        sdf = new SimpleDateFormat("dd MMM yyyy", Locale.getDefault());
        return sdf.format(dt);
0
votes

you could have a try like this:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);

change to :

SimpleDateFormat sdf1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss 'IST' yyyy", Locale.ENGLISH);
0
votes
String dateStr = "Sun Dec 06 11:15:00 IST 2015";
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(dateStr);
System.out.println(date);
0
votes

I was looking for answer why its not able to parce String Sun Dec 06 11:15:00 IST 2015 using SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy") other date formats like String date 01 10 2018 this with dd MM YYYY format able to parce but not above. So

I have tried this alternative way to get respective date format.

Hope this will help some one

value is date object

Date date   = d.parse(value);
Calendar c = Calendar.getInstance();
c.setTime(date);
int dd = c.get(Calendar.DAY_OF_MONTH);
int mmm = c.get(Calendar.MONTH) + 1;
int yyyy = c.get(Calendar.YEAR);

String dateString = "" + dd + "-" + mmm + "-" + yyyy;