1

What is the best way to calculate the difference between 01.01.2013 and now so the result looks like 25 days 16 hours 18 minutes 43 seconds?

Droidman
  • 11,485
  • 17
  • 93
  • 141
  • try using unix time: it is counted in seconds since 1.1.1970. so you can simply create 2 date objects and the difference of their unix-time will tell you how many seconds are between two dates. the rest is simple math. – devsnd Dec 05 '12 at 11:13
  • please refer my answer for this problem [solution][1] [1]: http://stackoverflow.com/questions/6897027/getting-difference-between-two-dates-android/16830015#16830015 – Swapnil Kadam May 30 '13 at 07:18

2 Answers2

2
String dateStop = "01.01.2013";
long now = System.currentTimeMillis():

SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");  


Date d1 = null;
Date d2 = null;
try {
     d1 = new Date (now);
     d2 = format.parse(dateStop);
} catch (ParseException e) {
     e.printStackTrace();
}   

long difference = d2.getTime() - d1.getTime();
long differenceBack = difference;
differenceBack = difference / 1000;
int secs = differenceBack % 60;
differenceBack /= 60;
int mins = differenceBack % 60;
differenceBack /= 60;
int hours = differenceBack % 24;

difference is in milliseconds. Then you can simply di some math to calculate days/hours/mins/seconds

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • thanks that works. Now I calculate the days like long days = difference/ 24 / 3600 / 1000; But I'm that stupid that I get some negative values while trying to calculate the remaining hours o_0 Can u help please? Sorry for asking dumb questions, just woken up and can't manage anything > – Droidman Dec 05 '12 at 11:34
0

Since you have not provided any code, I am not going to just do it for you however that said time can be an interesting topic for a foray of reasons (time zones for a start).

I would suggest this great library http://joda-time.sourceforge.net/

Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.

Look at using Periods and something along the lines

DateTime start = new DateTime(2004, 12, 25, 0, 0, 0, 0);
DateTime end = new DateTime(2006, 1, 1, 0, 0, 0, 0);

// period of 1 year and 7 days
Period period = new Period(start, end);

I used something similar to create a countdown clock, however you fail in your question to state what format (days, seconds, minutes) you want the value returned in. Period however is quite flexible on this front. For example:

// able to calculate whole days between two dates easily
Days days = Days.daysBetween(start, end);

// able to calculate whole months between two dates easily
Months months = Months.monthsBetween(start, end);
Graham Smith
  • 25,627
  • 10
  • 46
  • 69