多平台统一管理软件接口,如何实现多平台统一管理软件接口
299
2023-07-28
Java判断两个日期相差天数的方法
本文实例讲述了java判断两个日期相差天数的方法。分享给大家供大家参考。具体如下:
import java.util.Calendar;
public class DateDifferent{
public static void main(String[] args){
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.set(2007, 01, 10);
calendar2.set(2007, 07, 01CxfmP);
long milliseconds1 = calendar1.getTimeInMillis();
long milliseconds2 = calendar2.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long diffSeconds = diff / 1000;
long diffMinhttp://utes = diff / (60 * 1000);
long diffHours = diff / (60 * 60 * 1000);
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.println("\nThe Date Different Example");
System.out.println("Time in milliseconds: " + diff + " milliseconds.");
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");
System.out.println("Time in days: " + diffDays + " days.");
}
}
把上面的代码放在项目里使用了一下,【求日期部分】是要满24小时才算一天的,不太适应项目的需求,故改成这样子。
/**
* 得到两个日期相差的天数
*/
public static int getBetweenDay(Date date1, Date date2) {
Calendar d1 = new GregorianCalendar();
d1.setTime(date1);
Calendar d2 = new GregorianCalendar();
d2.setTime(date2);
int days = d2.get(Calendar.DAY_OF_YEAR)- d1.get(Calendar.DAY_OF_YEAR);
System.out.println("days="+days);
int y2 = d2.get(Calendar.YEAR);
http:// if (d1.get(Calendar.YEAR) != y2) {
// d1 = (Calendar) d1.clone();
do {
days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
d1.add(Calendar.YEAR, 1);
} while (d1.get(Calendar.YEAR) != y2);
}
return days;
}
希望本文所述对大家的java程序设计有所帮助。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~