开发者

Is there a function to calculate the difference between two times and display a relative result?

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 adva开发者_如何学Gonce.


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜