1

How I can get number of days between two dates including time. for example number of days between today 8pm till other day's 8pm? I am using this but it is giving according to day of month

public static int getDaysTillOmer(Date d1, Date d2) {
    int daysdiff = 0;
    long diff = d2.getTime() - d1.getTime();
    long diffDays = diff / (24 * 60 * 60 * 1000);
    daysdiff = (int) diffDays;

    return daysdiff;
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Ahmad Ayyaz
  • 774
  • 8
  • 25

4 Answers4

2

Use TimeUnit:

public static int getDaysTillOmer(Date d1, Date d2) {
    long diff = d2.getTime() - d1.getTime();
    return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
aliaksei
  • 714
  • 1
  • 9
  • 23
2

Your calculation (and the implementation in one of the answers) only works for system that run in a timezone that doesn't know daylight saving times. For e.g. the german timezone (where I reside), the duration of 2018-03-24 03:00:00 and 2018-03-25 03:00:00 I think you expect to be one day with the only problem that that particular day only has 23 hours.

If you want to stick with the standard classes, the JVM provides, you can use a Calendar and add day after day to it until the underlying date is after the second date. Here is an example (without sanity checks e.g. that the first date is actually before the second one; you will run into an endless loop in the other case). The example sets the timezone to be used explicitly, so it should show the same result on your system as well:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class CalcDuration {

    public final static void main(String[] args) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        Date d1 = sdf.parse("2018-03-24 03:00:00");
        Date d2 = sdf.parse("2018-03-25 03:00:00");

        System.out.println("duration1: " + getDuration(d1, d2));
    }

    private static int getDuration(Date d1, Date d2) {
        Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        c.setTime(d1);
        int days = 0;
        while (true) {
            c.add(Calendar.DAY_OF_YEAR, 1);
            if (c.getTime().after(d2)) {
                return days;
            }
            days++;
        }
    }
}
Lothar
  • 5,323
  • 1
  • 11
  • 27
1

Using Joda Time do it.

public void calcDays(Date start,Date end) {

        Duration d = new Duration(start.getTime(), end.getTime());


        System.out.println("Number of days from dates "+d.getStandardDays());
}

Duration is Class from JodaTime

Weslley Barbosa
  • 215
  • 3
  • 12
1

Assuming you are using java.util.Date your algorithm is quite good. I would clean it a little bit.

import java.util.Date;

public static int getDaysTillOmer(Date d1, Date d2) {
    long diffDays = (d2.getTime() - d1.getTime()) / (24L * 60 * 60 * 1000);
    return (int) diffDays;
}

This logic truncates the number of days to an integer value. That is a time difference of 2 days, 23 hours, and 59 minutes will be converted to: 2 days.

Also, note the value 24 was converted to 24L to prevent promotion to long.

The Impaler
  • 45,731
  • 9
  • 39
  • 76