0

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

9
  • 1
    Which version of SimpleDateFormat are you using java.text.SimpleDateFormat or android.icu.text.SimpleDateFormat? Commented Dec 12, 2018 at 6:10
  • @MorrisonChang java.text.SimpleDateFormat; format I am targeting minimum of version 16 Commented Dec 12, 2018 at 6:19
  • Have you tried Locale.US instead. Suggested by the Android documentation on Locale - Be wary of the default locale Commented Dec 12, 2018 at 7:34
  • @MorrisonChang yes I have tried Having issue on kitkat and lollipop rest its working Commented Dec 12, 2018 at 7:36
  • 1
    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? Commented Dec 12, 2018 at 14:01

4 Answers 4

0

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried but on its not working on some kitkat and lollipop device getting unable to parce
0

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);

Comments

0
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);

Comments

0

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;

1 Comment

Removing the z in the format worked for me. Not sure why! If anybody knows, please help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.