两个日期之间的日子比较 - >更简单的方法?

时间:2022-05-21 14:18:22

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 is on wednesday, the days between are 2 (the time does not matter)

因此,如果今天是星期一,我要比较的日期是星期三,之间的日子是2(时间无关紧要)

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?

在这段代码之后,我有一个叫做dayDiff的日子。但是真的必须制作日期日历,将时间设置为00:00:00:00并在日期保存到.getTime()吗?

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?

编辑:使用joda-time之后:joda-time是否也可以获取有关日期的信息,例如:差异== 1 ==>明天,或差异== -1 ==>昨天或我必须做什么手动吗?

5 个解决方案

#1


7  

You can use the JodaTime API as shown here.

您可以使用JodaTime API,如下所示。

#2


7  

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

对于指定的任务,我总是使用这种方便的方法:(没有lib,只是Java 5 API)

import java.util.concurrent.TimeUnit;

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

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

Enjoy!

请享用!

#3


3  

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

对不起我的粗心大意

#4


1  

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

您可以使用commons lang DateUtils.truncate,而不是将所有非相关值设置为0

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

无论如何,dayDiff(start-end)/ milliesPerDay将无法正常工作,因为Day Light Save更改。

#5


1  

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

这是一种不基于危险毫秒转换的分析daydiff方法:

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;
}

}

}

#1


7  

You can use the JodaTime API as shown here.

您可以使用JodaTime API,如下所示。

#2


7  

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

对于指定的任务,我总是使用这种方便的方法:(没有lib,只是Java 5 API)

import java.util.concurrent.TimeUnit;

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

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

Enjoy!

请享用!

#3


3  

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

对不起我的粗心大意

#4


1  

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

您可以使用commons lang DateUtils.truncate,而不是将所有非相关值设置为0

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

无论如何,dayDiff(start-end)/ milliesPerDay将无法正常工作,因为Day Light Save更改。

#5


1  

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

这是一种不基于危险毫秒转换的分析daydiff方法:

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;
}

}

}