1

I am trying to find a method within android of getting the time between the current date and a date set by the user. For example if they picked a date such as 14 june 2012 i would like my application to workout and display a count down such as "13 days remaining"

I currently have a date picker which will take a date from the user and display it, and i have it checking for the current date which it displays at the top of the screen.

I have seen some code on here that shows how do show how much time has past since a date but not sure how to change that to what i need.

Is there any method within android that could allow for this to be done?

Darren Murtagh
  • 591
  • 2
  • 6
  • 28

3 Answers3

0

This link shows how to do this using Joda, and I would agree with their response that you should use Joda instead of the standard (flawed) java APIs. I have used Joda with android before without any issues..

Community
  • 1
  • 1
Theblacknight
  • 575
  • 5
  • 12
  • i tried that within my class and eclipse seems to have a problem with imported the joda.time stuff. not sure why as i was testing the code given in the answer to be sure – Darren Murtagh Mar 07 '12 at 11:33
  • Download [joda](http://joda-time.sourceforge.net/), and put the jar in your lib folder, and add it to your build path. – Theblacknight Mar 07 '12 at 12:20
  • is that put into the folder within the eclipse file or into the application folder – Darren Murtagh Mar 07 '12 at 13:19
  • Check to see if you have a folder in your eclipse project called lib. If you do, put the jar into that folder. If not, create the folder (Right click the project, new>folder) and put the jar in. Then right click the jar and select "add to build path" – Theblacknight Mar 07 '12 at 15:09
0

One way would be to use the Calendar object and compare all the fields individually or by using a Date.

My answer would be use Calendar.getTimeInMillis(). Then use for example answers here: milliseconds to days to get from milliseconds to days or using TimeUnit

Community
  • 1
  • 1
David Olsson
  • 8,085
  • 3
  • 30
  • 38
0

You can use the android Time class: http://developer.android.com/reference/android/text/format/Time.html#set(int, int, int, int, int, int)

Create a time object and set it to the user specified date. Create another time object and use the setToNow() method to set it to the current time. You can then convert both to millis using the toMillis() method and subtract the chosen millis from the current millis, which will give you the millis difference. Then all you have to do is convert the difference into whatever unit of time you want to show. Days would be: difference/(1000ms/s * 60s/min * 60 min/hr * 24hr/day)

Sam Judd
  • 7,317
  • 1
  • 38
  • 38