Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    public static long DateDifference(String dateStart, String dateStop) {

        //String dateStart = "01/14/2012 09:29:58";
        //String dateStop = "01/15/2012 10:31:48";

        //HH converts hour in 24 hours format (0-23), day calculation
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Date d1 = null;
        Date d2 = null;
        long diffDays = -1;
        try {
            d1 = format.parse(dateStart);
            d2 = format.parse(dateStop);

            //in milliseconds
            long diff = d1.getTime() - d2.getTime();

            long diffSeconds = diff / 1000 % 60;
            long diffMinutes = diff / (60 * 1000) % 60;
            long diffHours = diff / (60 * 60 * 1000) % 24;
            diffDays = diff / (24 * 60 * 60 * 1000);

            /*   Log.v("days",diffDays + " days, ");
               Log.v("Hours",diffHours + " hours, ");
               Log.v("Minutes",diffMinutes + " minutes, ");
               Log.v("Seconds",diffSeconds + " seconds.");*/

        } catch (Exception e) {
            e.printStackTrace();
            diffDays = -1;
        }
        return diffDays;

    }
}