开发者

days between two dates compare -> easier way?

开发者 https://www.devze.com 2023-03-01 04:07 出处:网络
hi i\'m asking myself if there is an easier way to get the number of days between two dates. I want only the days, without looking at the hours or minutes.

hi i'm asking myself if there is an easier way to get the number of days between two dates.

I want only the days, without looking at the hours or minutes.

Therefore if today is monday, and the date wich i want to compare 开发者_StackOverflow社区is on wednesday, the days between are 2 (the time does not matter)

Therefore i use this code:

        Calendar c = Calendar.getInstance();
        // Only the day:
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        Calendar to = Calendar.getInstance();
        to.setTime(date);
        to.set(Calendar.HOUR, 0);
        to.set(Calendar.MINUTE, 0);
        to.set(Calendar.SECOND, 0);
        to.set(Calendar.MILLISECOND, 0);
        date = to.getTime();

        long millsPerDay = 1000 * 60 * 60 * 24;

        long dayDiff = ( date.getTime() - dateToday.getTime() ) / millsPerDay;

after this code i have the days in a long called dayDiff. but is it really necessarily to make a calendar of the date, set the time to 00:00:00:00 and save to.getTime() in date?

Edit: After using joda-time: Is it also possible with joda-time to get information about the days, like: difference==1 ==> Tomorrow, or difference == -1 ==> yesterday or do I have to do that manually?


You can use the JodaTime API as shown here.


For specified task I always use this convenient way: (no lib, just Java 5 API)

import java.util.concurrent.TimeUnit;

Date d1 = ...
Date d2 = ...

long daysBetween = TimeUnit.MILLISECONDS.toDays(d2.getTime() - d1.getTime());

Enjoy!


public long dayDiff(Date d1, Date d2) {
    final long DAY_MILLIS = 1000 * 60 * 60 * 24;
    long day1 = d1.getTime() / DAY_MILLIS;
    long day2 = d2.getTime() / DAY_MILLIS;
    return (day1 - day2);
}

Sorry for my carelessness


Instead of setting all not relavant values to 0, you can use commons lang DateUtils.truncate

Anyway, the dayDiff (start-end)/milliesPerDay will not work correct, because of Day Light Save changes.


Here is an analytical daydiff method not based on hazardous millisecond-conversions :

public static int dayDiff(Calendar to, Calendar from){
    int result = 0;
    int years;


    // global year difference from 1.jan to 1.jan
    years = to.get(Calendar.YEAR) - from.get(Calendar.YEAR);
    result = years * 365; 

    // adding days for simple leap years ( divisible by 4 ). This an approximation that will be corrected by the negative leap years formula.
    result += (to.get(Calendar.YEAR)-1)/4 - (from.get(Calendar.YEAR)-1)/4;

    // removing days for negative leap years ( divisible by 100 ). This is still an approximation that will be corrected by the big leap years formula.
    result -= (to.get(Calendar.YEAR)-1)/100 - (from.get(Calendar.YEAR)-1)/100;

    // adding days for big leap years ( divisible by 400 ). After this formula, the days count from 1.jan.<from> to 1.jan.<to> is correct.
    result += (to.get(Calendar.YEAR)-1)/400 - (from.get(Calendar.YEAR)-1)/400;

    // adding month of to-year
    for(int m=0; m<to.get(Calendar.MONTH ); m++){
        result += daysInMonth(m, to.get(Calendar.YEAR));
    }

    // substracting month of from-year
    for(int m=0; m<from.get(Calendar.MONTH ); m++){
        result -= daysInMonth(m, from.get(Calendar.YEAR));
    }

    // adding days of to-year
    result += to.get(Calendar.DAY_OF_MONTH ); 


    // substracting days of from-year
    result -= from.get(Calendar.DAY_OF_MONTH ); 

    return result;

}

private static int daysInMonth(int m, int y){
    if(m==3 || m==5 || m==8 || m==10) return 30;
    if(m==1)
        if(isLeapYear(y)) return 29;
        else return 28;
    return 31;
}


private static boolean isLeapYear(int y){
    return (isSimpleLeapYear(y) && !isNegativeLeapYear(y)) || isBigLeapYear(y);
}

private static boolean isSimpleLeapYear(int y){
    return y%4 == 0;
}

private static boolean isNegativeLeapYear(int y){
    return y%100 == 0;
}

private static boolean isBigLeapYear(int y){
    return y%400 == 0;
}

}

0

精彩评论

暂无评论...
验证码 换一张
取 消