java日期相减得到天数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java日期相减得到天数相关的知识,希望对你有一定的参考价值。
public class Dat static SimpleDateFormat h; public static void Test(Date date, int m) h = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String n = h.format(date); String myString = DateFormat.getDateInstance().format(date); System.out.println("格式化后:" + myString); Timestamp time = Timestamp.valueOf(n); // 在天数上加(减)天数 long l = time.getTime() + 24 * 60 * 60 * m * 1000; time.setTime(l); System.out.println("计算后的日期:" + time); public static void main(String arg[]) Date date=new Date(108,04,02); Test(date,5); 这个是加上天数的,怎么实现相减?有点问题
参考技术A import java.text.DateFormat;import java.text.SimpleDateFormat;
import java.util.*;
public class Dat
private static SimpleDateFormat h;
private static Map<String ,Integer> map = new HashMap<String, Integer>();
static
map.put("date", Calendar.DATE);
map.put("month", Calendar.MONTH);
map.put("year", Calendar.YEAR);
map.put("hour", Calendar.HOUR);
map.put("minute", Calendar.MINUTE);
map.put("second", Calendar.SECOND);
map.put("millisecond", Calendar.MILLISECOND);
map.put("天", Calendar.DATE);
map.put("月", Calendar.MONTH);
map.put("年", Calendar.YEAR);
map.put("时", Calendar.HOUR);
map.put("分", Calendar.MINUTE);
map.put("秒", Calendar.SECOND);
map.put("毫", Calendar.MILLISECOND);
public static void Test(Date date,String type, int number)
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(map.get(type), number);
Date newDate = cal.getTime();
System.out.println(DateFormat.getDateTimeInstance().format(newDate) + " " + date.getTime()%1000);
public static void main(String arg[])
//过时方法请不要使用
Date date=new Date();
Test(date,"date",5);
Test(date,"分",5);
Test(date,"minute",-5);
JS 时间相减得出天数
需要完整代码
// 给日期类对象添加日期差方法,返回日期与diff参数日期的时间差,单位为天Date.prototype.diff = function(date)
return (this.getTime() - date.getTime())/(24 * 60 * 60 * 1000);
// 构造两个日期,分别是系统时间和2013/04/08 12:43:45
var now = new Date();
var date = new Date(\'2013/04/08 12:43:45\');
// 调用日期差方法,求得参数日期与系统时间相差的天数
var diff = now.diff(date);
// 打印日期差
alert(diff); 参考技术A function isLeapYear(year)
if (year % 4 == 0 && ((year % 100 != 0) || (year % 400 == 0)))
return true;
return false;
function validatePeriod(fyear, fmonth, fday, byear, bmonth, bday)
if (fyear < byear)
return true;
else if (fyear == byear)
if (fmonth < bmonth)
return true;
else if (fmonth == bmonth)
if (fday <= bday)
return true;
else
return false;
else
return false;
else
return false;
function dateDiff(d1, d2)
var disNum = compareDate(d1, d2);
return disNum;
function compareDate(date1, date2)
var regexp = /^(\d1,4)[-|\.]1(\d1,2)[-|\.]1(\d1,2)$/;
var monthDays = [0, 3, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1];
regexp.test(date1);
var date1Year = RegExp.$1;
var date1Month = RegExp.$2;
var date1Day = RegExp.$3;
regexp.test(date2);
var date2Year = RegExp.$1;
var date2Month = RegExp.$2;
var date2Day = RegExp.$3;
if (validatePeriod(date1Year, date1Month, date1Day, date2Year, date2Month, date2Day))
firstDate = new Date(date1Year, date1Month, date1Day);
secondDate = new Date(date2Year, date2Month, date2Day);
result = Math.floor((secondDate.getTime() - firstDate.getTime()) / (1000 * 3600 * 24));
for (j = date1Year; j <= date2Year; j++)
if (isLeapYear(j))
monthDays[1] = 2;
else
monthDays[1] = 3;
for (i = date1Month - 1; i < date2Month; i++)
result = result - monthDays[i];
return result;
return;
days = dateDiff('2013-04-08', '2013-04-9') + 1;
以上是关于java日期相减得到天数的主要内容,如果未能解决你的问题,请参考以下文章