4

Actually i am stuck into minor issue but not getting any proper solution. I have two dates in same format now i want difference between but the format would be (Year - Month - Days - Hours - Mints - Sec) and here is my code

  public static void getDifferent(Date startDate, Date endDate) {

        //milliseconds
        long different = endDate.getTime() - startDate.getTime();


        System.out.println("startDate : " + startDate);
        System.out.println("endDate : " + endDate);
        System.out.println("different : " + different);

        long secondsInMilli = 1000;
        long minutesInMilli = secondsInMilli * 60;
        long hoursInMilli = minutesInMilli * 60;
        long daysInMilli = hoursInMilli * 24;
        long monthInMili = daysInMilli * 30;

        long elapsedMonths = different / monthInMili;

        long elapsedDays = different / daysInMilli;
        different = different % daysInMilli;

        long elapsedHours = different / hoursInMilli;
        different = different % hoursInMilli;

        long elapsedMinutes = different / minutesInMilli;
        different = different % minutesInMilli;

        long elapsedSeconds = different / secondsInMilli;

        Log.d("Difference", "startDate: " + startDate.toString() + " endDate: " + endDate + " Months : " + elapsedMonths + " Days :" + elapsedDays + " Hours :" +
                elapsedHours + " Mint :" + elapsedMinutes + " Seconds :" + elapsedSeconds);


    }

This is code is not perfect due to this line

long monthInMili = daysInMilli * 30;

So please guide me. I am not getting proper solution from long time.

Rujul Gandhi
  • 1,700
  • 13
  • 28

2 Answers2

4

Edit: To get the months difference, you can use Calender class. Otherwise, use JodaTime library(linked at bottom of answer).

Calendar startCalendar = new GregorianCalendar();
startCalendar.setTime(startDate);
Calendar endCalendar = new GregorianCalendar();
endCalendar.setTime(endDate);

int diffYear = endCalendar.get(Calendar.YEAR) - startCalendar.get(Calendar.YEAR);
int diffMonth = endCalendar.get(Calendar.MONTH) - startCalendar.get(Calendar.MONTH);
int diffDay = endCalendar.get(Calendar.DAY_OF_MONTH) - startCalendar.get(Calendar.DAY_OF_MONTH);

Note that if your dates are 2013-01-31 and 2013-02-01, you get a distance of 1 month this way, which may or may not be what you want. To correct this, refer to this answer.

Before Edit

Just subtract the dates and calculate the milliseconds according to your needs

// Start Date : 01/14/2012 09:29:58
// End Date   : 01/15/2012 10:31:48

public static void getDifference(Date d1, Date d2){

    // HH converts hour in 24 hours format (0-23), day calculation
    // must match with your date format
    SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

    try {

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

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

        System.out.print(diffDays + " days, ");
        System.out.print(diffHours + " hours, ");
        System.out.print(diffMinutes + " minutes, ");
        System.out.print(diffSeconds + " seconds.");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

You can also use joda-time-library and follow this tutorial's second step to implement it.

Community
  • 1
  • 1
rupinderjeet
  • 2,984
  • 30
  • 54
  • suppose in case start date = 01/14/2012 09:29:58 and end date = 01/5/2011 09:29:58. now in this condition i am getting some large value of days RIght ? i want to days in range of 0-30. and want extra field as month like for 12 month 3 days 1 hour 1 mint 1 sec i want like this. – Rujul Gandhi Nov 26 '16 at 10:47
  • i think you are near by my answer but not still perfect. when i put start date and end date difference will be exact 1 year then you function will give me 1 year and 12 month. I want perfect 1 year and 0 months. – Rujul Gandhi Nov 26 '16 at 11:05
  • just remove `diffYear * 12 + ` from `int diffMonth = ` line. Updated the answer. – rupinderjeet Nov 26 '16 at 11:07
-1

you have to write code like this

    long different = todayDate.getTime() - meetingDate.getTime();

    long secondsInMilli = 1000;
    long minutesInMilli = secondsInMilli * 60;
    long hoursInMilli = minutesInMilli * 60;
    long daysInMilli = hoursInMilli * 24;
    long mothsInMilli = daysInMilli * 30;
    long yearInMilli = mothsInMilli * 12;

    long elapsedYear = different / yearInMilli;
    different = different % yearInMilli;
    System.out.println("--------- elapsedYear : " + elapsedYear);

    long elapsedMonths = different / mothsInMilli;
    different = different % mothsInMilli;
    System.out.println("--------- elapsedMonths : " + elapsedMonths);

    long elapsedDays = different / daysInMilli;
    different = different % daysInMilli;
    System.out.println("--------- elapsedDays : " + elapsedDays);

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;
    System.out.println("--------- elapsedHours : " + elapsedHours);

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;
    System.out.println("--------- elapsedMinutes : " + elapsedMinutes);

    long elapsedSeconds = different / secondsInMilli;
    System.out.println("--------- elapsedSeconds : " + elapsedSeconds);
  • Every month does not have 30 days. There are months having 28 & 31 days as well. In this case, above code will not calculate difference accurately. – rupinderjeet Nov 26 '16 at 10:58
  • I know but in this case we have Different only in Milli Seconds. so with only Milli Seconds we can't specify how many day in months so we have to guess 30 days in every month. – Bhargav Ghodasara Nov 26 '16 at 11:02