Difference in days between two dates in Java?

后端 未结 19 1724
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 08:26

I need to find the number of days between two dates: one is from a report and one is the current date. My snippet:

  int age=calculateDiffer         


        
相关标签:
19条回答
  • 2020-11-22 08:35

    If you're looking for a solution that returns proper number or days between e.g. 11/30/2014 23:59 and 12/01/2014 00:01 here's solution using Joda Time.

    private int getDayDifference(long past, long current) {
        DateTime currentDate = new DateTime(current);
        DateTime pastDate = new DateTime(past);
        return currentDate.getDayOfYear() - pastDate.getDayOfYear();
    } 
    

    This implementation will return 1 as a difference in days. Most of the solutions posted here calculate difference in milliseconds between two dates. It means that 0 would be returned because there's only 2 minutes difference between these two dates.

    0 讨论(0)
  • 2020-11-22 08:36

    Hundred lines of code for this basic function???

    Just a simple method:

    protected static int calculateDayDifference(Date dateAfter, Date dateBefore){
        return (int)(dateAfter.getTime()-dateBefore.getTime())/(1000 * 60 * 60 * 24); 
        // MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
    }
    
    0 讨论(0)
  • 2020-11-22 08:38
    public static int getDifferenceIndays(long timestamp1, long timestamp2) {
        final int SECONDS = 60;
        final int MINUTES = 60;
        final int HOURS = 24;
        final int MILLIES = 1000;
        long temp;
        if (timestamp1 < timestamp2) {
            temp = timestamp1;
            timestamp1 = timestamp2;
            timestamp2 = temp;
        }
        Calendar startDate = Calendar.getInstance(TimeZone.getDefault());
        Calendar endDate = Calendar.getInstance(TimeZone.getDefault());
        endDate.setTimeInMillis(timestamp1);
        startDate.setTimeInMillis(timestamp2);
        if ((timestamp1 - timestamp2) < 1 * HOURS * MINUTES * SECONDS * MILLIES) {
            int day1 = endDate.get(Calendar.DAY_OF_MONTH);
            int day2 = startDate.get(Calendar.DAY_OF_MONTH);
            if (day1 == day2) {
                return 0;
            } else {
                return 1;
            }
        }
        int diffDays = 0;
        startDate.add(Calendar.DAY_OF_MONTH, diffDays);
        while (startDate.before(endDate)) {
            startDate.add(Calendar.DAY_OF_MONTH, 1);
            diffDays++;
        }
        return diffDays;
    }
    
    0 讨论(0)
  • 2020-11-22 08:43

    It depends on what you define as the difference. To compare two dates at midnight you can do.

    long day1 = ...; // in milliseconds.
    long day2 = ...; // in milliseconds.
    long days = (day2 - day1) / 86400000;
    
    0 讨论(0)
  • 2020-11-22 08:45

    java.time

    In Java 8 and later, use the java.time framework (Tutorial).

    Duration

    The Duration class represents a span of time as a number of seconds plus a fractional second. It can count days, hours, minutes, and seconds.

    ZonedDateTime now = ZonedDateTime.now();
    ZonedDateTime oldDate = now.minusDays(1).minusMinutes(10);
    Duration duration = Duration.between(oldDate, now);
    System.out.println(duration.toDays());
    

    ChronoUnit

    If all you need is the number of days, alternatively you can use the ChronoUnit enum. Notice the calculation methods return a long rather than int.

    long days = ChronoUnit.DAYS.between( then, now );
    
    0 讨论(0)
  • 2020-11-22 08:46

    I would suggest you use the excellent Joda Time library instead of the flawed java.util.Date and friends. You could simply write

    import java.util.Date;
    import org.joda.time.DateTime;
    import org.joda.time.Days;
    
    Date past = new Date(110, 5, 20); // June 20th, 2010
    Date today = new Date(110, 6, 24); // July 24th 
    int days = Days.daysBetween(new DateTime(past), new DateTime(today)).getDays(); // => 34
    
    0 讨论(0)
提交回复
热议问题