Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.Calendar;

public class Main {
    public static String getShortDuration(Calendar startDate, Calendar endDate) {
        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();
        }

    }
}