0

I am very much new to Android. 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)
halfer
  • 19,824
  • 17
  • 99
  • 186
Nik
  • 2,913
  • 7
  • 40
  • 66
  • Substracting dates in Java is a much discussed topic. I suggest you [this Q&A](http://stackoverflow.com/questions/3526485/how-do-you-subtract-dates-in-java) for the built-in Java approach and the one using JodaTime. – MH. Apr 15 '12 at 11:48
  • is your issue fixed are still facing any issue – Shankar Agarwal Apr 15 '12 at 12:02

2 Answers2

1

Add the format of the date you want to parse:

 passDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2012-08-17 00:00:00");
Nesim Razon
  • 9,684
  • 3
  • 36
  • 48
1
Date currentDate = new Date(System.currentTimeMillis());
Log.v("@@@@@@@@@","Current Date: " + currentDate);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Date passDate = dateFormat.parse(passDateString);
Log.v("@@@@@@@@@","Pass Date: " + passDate);
dateDifference = passDate.compareTo(currentDate);

use this LINK to get difference between two dates and currently now you are just comparing the dates.

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • your code worked but the difference I am getting is not right.Here are the logs 04-15 12:39:42.701: V/@@@@@@@@@(1421): Current Date: Sun Apr 15 12:39:42 GMT+01:00 2012 04-15 12:39:42.701: V/@@@@@@@@@(1421): Pass Date: Thu Aug 23 00:00:00 GMT+01:00 2012 04-15 12:39:42.711: V/@@@@@@@@@(1421): Date Difference: 1 – Nik Apr 15 '12 at 11:40
  • you are comparing two date not getting the differece and you want diffrence in months/days/weeks/millisecond based on that you have write code accordingly – Shankar Agarwal Apr 15 '12 at 11:42
  • I dint get you... what you trying to say... say I want the difference between the 2 dates in days and months and year. if not all atleast in for my days – Nik Apr 15 '12 at 11:44
  • compare to return 1 if first date is getter then second, and 0 if both dates are equal and -1 if first date is less than second – Shankar Agarwal Apr 15 '12 at 11:46
  • Ok Got it... So how can I get what I want... Can you please share a code piece in my case – Nik Apr 15 '12 at 11:47
  • better check and use joda-time for date operations http://joda-time.sourceforge.net/ – Nesim Razon Apr 15 '12 at 11:50