-3

I am trying to convert string to date inside Asynctask by parsing json string. I referred this link: How to convert "Mon Jun 18 00:00:00 IST 2012" to 18/06/2012?

//String dateStr = "Mon Jun 18 00:00:00 IST 2012";

changedDate = jsonObjectMain.getString("date");
DateFormat formatter = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");
Date date = (Date)formatter.parse(changedDate);
System.out.println(date);        

Calendar cal = Calendar.getInstance();
cal.setTime(date);
String formatedDate = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" +         cal.get(Calendar.YEAR);
System.out.println("formatedDate : " + formatedDate);  

When I run this code in android studio, it gives me below exception:

04-14 12:35:53.363 22963-22963/com.trial W/System.err: java.text.ParseException: Unparseable date: "Mon Jun 18 00:00:00 IST 2012" (at offset 20) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at java.text.DateFormat.parse(DateFormat.java:579) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at com.trial.Activity.StartActivity.onClick(StartActivity.java:124) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at android.view.View.performClick(View.java:5210) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at android.view.View$PerformClick.run(View.java:21183) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at android.os.Looper.loop(Looper.java:148) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5438) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at java.lang.reflect.Method.invoke(Native Method) 04-14 12:35:53.367 22963-22963/com.trialW/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762) 04-14 12:35:53.367 22963-22963/com.trial W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

But when i run same code in eclipse by creating simple program, it runs very well.

I was wondered, I don't know, why is this happen. So, i checked same code in another Activity on button click but not worked.

Thank you. Any help will be appreciated.

Community
  • 1
  • 1
Kuldeep Kulkarni
  • 796
  • 5
  • 20
  • Convert IST string is quite tricky. I do believe the error you get is because of the 'IST'. May I know if the timezone is fix on IST or dynamic? – Amad Yus Apr 14 '17 at 09:13
  • I am getting string from json with this formate "Mon Jun 18 00:00:00 IST 2012". So i am not sure about timezone is fix or not. – Kuldeep Kulkarni Apr 14 '17 at 09:16
  • 1
    If you can, stay away from three letter time zone abbreviations. IST may mean India Standard Time, Irish Standard Time or Israel Standard Time. Better to use for instance `Asia/Kolkata`, `Europe/Dublin` or `Asia/Tel_Aviv`. – Ole V.V. Apr 14 '17 at 11:39
  • Ok, will take care of it next time. Thanks. – Kuldeep Kulkarni Apr 14 '17 at 11:46
  • @KuldeepKulkarni The down-votes may be due to you posting without bothering to search any of the hundreds (thousands?) of Questions and Answers already posted on this topic. – Basil Bourque Apr 22 '17 at 06:21

2 Answers2

1

I am aware that using the newer java.time classes on Android requires an external dependency (ThreeTenABP), and also that you have a working solution by now. For anyone else reading along that either can use Java 8 or later or are happy with a library dependency, here’s how to do:

String dateStr = "Mon Jun 18 00:00:00 IST 2012";

DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("EEE MMM d HH:mm:ss z uuuu", Locale.ENGLISH);
LocalDate date = LocalDate.parse(dateStr, inputFormat);

String formattedDate = date.format(DateTimeFormatter.ofPattern("dd/MM/uuuu"));

System.out.println(formattedDate);

If you know in advance that the string contains correct day, month and year, you can ignore the time zone completely.

Link: The java.time classes (of which LocalDate is just one among many helpful) can be used on Android throught the ThreeTenABP. That’s ThreeTen for JSR-310 and ABP for Android Backport.

PS If you want some further validation of your input string, you may for instance check that time part equals the start of the day in the specified time zone:

ZonedDateTime zdt = ZonedDateTime.parse(dateStr, inputFormat);
if (! zdt.equals(zdt.toLocalDate().atStartOfDay(zdt.getZone()))) {
    throw new IllegalArgumentException("Input date was not at midnight");
}
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

Try changing format to "EEE MMM dd HH:mm:ss yyyy" and set IST timeZone

changedDate = changedDate.replace("IST ", "");
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyy");
TimeZone timeZone=TimeZone.getTimeZone("IST");
formatter.setTimeZone(timeZone);

Date date = (Date)formatter.parse(changedDate);
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • FYI These troublesome classes are now legacy, supplanted by the java.time classes. See the *ThreeTenABP* project for Android. – Basil Bourque Apr 14 '17 at 20:52