How do compare dates when one of those are in string format in android

Posted by Raj on Stack Overflow See other posts from Stack Overflow or by Raj
Published on 2012-04-15T11:20:10Z Indexed on 2012/04/15 11:29 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

I am very much new to android so need some good help with a code example.

I am getting a date in form of string from a server in the following format

2012-08-17 00:00:00

I want to compare this string with current date to find the difference between the dates in the form of year, months and days...

I tried playing around it in the following code

Date currentDate = new Date(System.currentTimeMillis());
Log.v("@@@@@@@@@","Current Date: " + currentDate);
Date passDate = new SimpleDateFormat().parse(passDateString);
Log.v("@@@@@@@@@","Pass Date: " + passDate);
dateDifference = passDate.compareTo(currentDate);

but it returned with following exception

04-15 12:08:29.101: V/@@@@@@@@@(1161): Current Date: Sun Apr 15 12:08:29 GMT+01:00 2012
04-15 12:08:29.101: W/System.err(1161): java.text.ParseException: Unparseable date: 2012-08-17 00:00:00
04-15 12:08:29.111: W/System.err(1161):     at java.text.DateFormat.parse(DateFormat.java:645)
04-15 12:08:29.111: W/System.err(1161):     at org.apis.PassesListItemAdapter.getView(PassesListItemAdapter.java:77)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.ListView.fillDown(ListView.java:652)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.ListView.fillFromTop(ListView.java:709)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.ListView.layoutChildren(ListView.java:1580)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
04-15 12:08:29.111: W/System.err(1161):     at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
04-15 12:08:29.111: W/System.err(1161):     at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-15 12:08:29.111: W/System.err(1161):     at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
04-15 12:08:29.111: W/System.err(1161):     at android.view.View.layout(View.java:7034)
04-15 12:08:29.111: W/System.err(1161):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1049)
04-15 12:08:29.111: W/System.err(1161):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1744)
04-15 12:08:29.111: W/System.err(1161):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-15 12:08:29.111: W/System.err(1161):     at android.os.Looper.loop(Looper.java:144)
04-15 12:08:29.111: W/System.err(1161):     at android.app.ActivityThread.main(ActivityThread.java:4937)
04-15 12:08:29.111: W/System.err(1161):     at java.lang.reflect.Method.invokeNative(Native Method)
04-15 12:08:29.111: W/System.err(1161):     at java.lang.reflect.Method.invoke(Method.java:521)
04-15 12:08:29.111: W/System.err(1161):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-15 12:08:29.111: W/System.err(1161):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-15 12:08:29.111: W/System.err(1161):     at dalvik.system.NativeStart.main(Native Method)

I am stuck... please help

Raj

© Stack Overflow or respective owner

Related posts about android

Related posts about string