java 怎么计算当前时间 到 某一给定时间的间隔天数
5个回答
展开全部
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//给定的时间
Date d=format.parse("2013-03-21 23:59:00");
//当前时间处理
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
//给定时间处理
Calendar setCal = Calendar.getInstance();
setCal.setTime(d);
setCal.set(Calendar.HOUR_OF_DAY, 0);
setCal.set(Calendar.MINUTE, 0);
setCal.set(Calendar.SECOND, 0);
setCal.set(Calendar.MILLISECOND, 0);
long dayDiff =(setCal.getTimeInMillis()-cal.getTimeInMillis())/(1000*60*60*24);
System.out.println(dayDiff);
//给定的时间
Date d=format.parse("2013-03-21 23:59:00");
//当前时间处理
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
//给定时间处理
Calendar setCal = Calendar.getInstance();
setCal.setTime(d);
setCal.set(Calendar.HOUR_OF_DAY, 0);
setCal.set(Calendar.MINUTE, 0);
setCal.set(Calendar.SECOND, 0);
setCal.set(Calendar.MILLISECOND, 0);
long dayDiff =(setCal.getTimeInMillis()-cal.getTimeInMillis())/(1000*60*60*24);
System.out.println(dayDiff);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简直用当前时间-给定时间,就可以得出
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
当前时间就是new Date()
你这个相当如计算两个时间之间的间隔
/**
* 获取两个时间的时间间隔
*
* @param beginDate
* 开始时间
* @param endDate
* 结束时间
* @return
*/
public static int getDaysBetween(Calendar beginDate, Calendar endDate) {
if (beginDate.after(endDate)) {
Calendar swap = beginDate;
beginDate = endDate;
endDate = swap;
}
int days = endDate.get(Calendar.DAY_OF_YEAR)
- beginDate.get(Calendar.DAY_OF_YEAR) + 1;
int year = endDate.get(Calendar.YEAR);
if (beginDate.get(Calendar.YEAR) != year) {
beginDate = (Calendar) beginDate.clone();
do {
days += beginDate.getActualMaximum(Calendar.DAY_OF_YEAR);
beginDate.add(Calendar.YEAR, 1);
} while (beginDate.get(Calendar.YEAR) != year);
}
return days;
}
你这个相当如计算两个时间之间的间隔
/**
* 获取两个时间的时间间隔
*
* @param beginDate
* 开始时间
* @param endDate
* 结束时间
* @return
*/
public static int getDaysBetween(Calendar beginDate, Calendar endDate) {
if (beginDate.after(endDate)) {
Calendar swap = beginDate;
beginDate = endDate;
endDate = swap;
}
int days = endDate.get(Calendar.DAY_OF_YEAR)
- beginDate.get(Calendar.DAY_OF_YEAR) + 1;
int year = endDate.get(Calendar.YEAR);
if (beginDate.get(Calendar.YEAR) != year) {
beginDate = (Calendar) beginDate.clone();
do {
days += beginDate.getActualMaximum(Calendar.DAY_OF_YEAR);
beginDate.add(Calendar.YEAR, 1);
} while (beginDate.get(Calendar.YEAR) != year);
}
return days;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
相减就可以!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询