Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.Date;

public class Main {
    /**
     * 
     * @param d1
     * @param d2
     * @return
     */
    public static String getDateDifference(Date d1, Date d2) {
        String unit = "seconds";
        long diff = d2.getTime() - d1.getTime();
        diff = diff / 1000;

        // /* More than 7 days? Then go to weeks */
        // if (diff >= (60 * 60 * 24 * 7)) {
        // diff = diff / 7;
        // unit = "weeks";
        // }

        /* More than 72 hours? Then go to days */
        if (diff >= (60 * 60 * 24 * 3)) {
            diff = diff / 24;
            unit = "days";
        }

        /* More than 60 minutes? Then go to hours */
        if (diff >= (60 * 60) && diff < (60 * 60 * 24 * 3)) {
            diff = diff / 60;
            unit = "hours";
        }

        /* More than 60 seconds? Then go to minutes */
        if (diff >= 60 && diff < (60 * 60)) {
            diff = diff / 60;
            unit = "minutes";
        }

        return diff + " " + unit;
    }
}