tags:

views:

36

answers:

2

I have a DB sorted by date, and in my output, I would like to display the elapsed time between rows.. like this..

| Entry #1 on 08/10/2010 08:34 AM

| Entry #2 on 08/10/2010 09:45 AM | 1 hour 11 minutes since previous entry

| Entry #3 on 08/11/2010 06:57 AM | 1 day 3 hours 12 minutes since previous entry

| Entry #4 on 08/11/2010 08:34 PM | 13 hours 37 hours since previous entry

| Entry #5 on 09/11/2011 08:48 PM | 1 year 1 month 8 minutes since previous entry

I've looked at getRelativeTimeSpanString() but 1) it calculates from now & 2) it appends a prefix/suffix.

Is there an easier way to do this, other than creating a custom datediff method and chopping all of that together with conditions myself?

Thanks in advance.

A: 

see this example and implement yours.. :)

Vinay
+1  A: 

I didn't find anything in the Android SDK which does a similar job (Google included one in the GWT framework...maybe one could take it out there).

But since it is not too difficult I wrote a DateUtils class myself which does a similar job. I like the friendly date formats which are also used in Gmail like "Yesterday" or "2 min ago" rather than a date like 13/08/2010 or 10:30 PM.

To what regards time differences I wrote the following

public static String getDifference(Date sessionStart, Date sessionEnd) {
    if(sessionStart == null)
        return "[corrupted]";

    Calendar startDateTime = Calendar.getInstance();
    startDateTime.setTime(sessionStart);

    Calendar endDateTime = Calendar.getInstance();
    endDateTime.setTime(sessionEnd);

    long milliseconds1 = startDateTime.getTimeInMillis();
    long milliseconds2 = endDateTime.getTimeInMillis();
    long diff = milliseconds2 - milliseconds1;

    long hours = diff / (60 * 60 * 1000);
    long minutes = diff / (60 * 1000);
    minutes = minutes - 60 * hours;
    long seconds = diff / (1000);

    if (hours > 0) {
        return hours + " hours " + minutes + " minutes";
    } else {
        if (minutes > 0)
            return minutes + " minutes";
        else {
            return seconds + " seconds";
        }
    }
}

..and here is the according unit test to verify the correct functioning:

    @Test
    public void testGetDifference() {
        // hours and minutes
        Date startDate = DateUtils.createDateInstance("01.01.2010 12:00:00");
        Date endDate = DateUtils.createDateInstance("01.01.2010 13:12:00");

        String difference = DateUtils.getDifference(startDate, endDate);
        assertNotNull(difference);
        assertEquals("1 hours 12 minutes", difference);

        // minutes
        startDate = DateUtils.createDateInstance("01.01.2010 12:00:00");
        endDate = DateUtils.createDateInstance("01.01.2010 12:02:00");

        difference = DateUtils.getDifference(startDate, endDate);
        assertNotNull(difference);
        assertEquals("2 minutes", difference);

        // seconds
        startDate = DateUtils.createDateInstance("01.01.2010 12:00:00");
        endDate = DateUtils.createDateInstance("01.01.2010 12:00:15");

        difference = DateUtils.getDifference(startDate, endDate);
        assertNotNull(difference);
        assertEquals("15 seconds", difference);

        difference = DateUtils.getDifference(null, endDate);
        assertEquals("[corrupted]", difference);
    }

You may have to tune it a bit and also externalize the hardcoded strings ("hours", "minutes",...) in order to make everything easier to localize (if that's an issue for you).

Just as a reference, it may probably also be worth looking at the JodaTime lib. Maybe they have a similar implementation, but I didn't verify that myself (yet).

Juri
Thanks for this!.. I figured I would have to hack something together myself.. Your code is a great start though.. just looks like i have to add days, weeks, months, years code.. yay! fun! :p
kefs
I ended up modifying and using the following class:http://www.technojeeves.com/joomla/index.php/free/71-difference-between-two-dates-in-java
kefs
Nice. Just as a side note. Write a unit test instead of a dummy main() printing to the console. In that way you already have a verification of whether your code works correctly :)
Juri