get Short Duration between two Calendar - Android java.util

Android examples for java.util:Calendar

Description

get Short Duration between two Calendar

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    public static String getShortDuration(Calendar startDate,
            Calendar endDate) {//from   w  w w .j ava 2  s .co  m
        long startTime = startDate.getTimeInMillis();
        long endTime = endDate.getTimeInMillis();
        long milliseconds = endTime - startTime;

        int days = (int) (milliseconds / (1000 * 60 * 60 * 24));
        int hours = (int) (milliseconds / (1000 * 60 * 60));
        int min = (int) (milliseconds - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))
                / (1000 * 60);

        if (hours < 1) {
            return min + " min";
        } else {
            StringBuilder durationText = new StringBuilder();
            if (hours == 1) {
                durationText.append(hours + " hr");
            } else {
                durationText.append(hours + " hrs");
            }

            if (min > 30) {
                durationText.append(" " + min + " min");
            }

            return durationText.toString();
        }

    }
}

Related Tutorials