java日期相减,返回天数
public long fromDateStringToLong(String inVal) {
Date date = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
date = inputFormat.parse(inVal);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
}
public String fromLongToDate(long inVal) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date currentTime = new Date(inVal);
return sdf.format(currentTime);
}
public int setDate(){
String str="";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dt=new Date();
str=format1.format(dt);
long startT=fromDateStringToLong(str);
long endT=fromDateStringToLong("2011-01-27 00:00:00");//活动开始时间
// long fin=df.fromDateStringToLong( "1970-01-01 0:00 ");
// System.out.println(startT+ "== "+endT+ "== "+(startT-endT)+ "== "+fin);
long mint=(startT-endT)/(1000);
int hor=(int)mint/3600;
int secd=(int)mint%3600;
int day=(int)hor/24;
System.out.println("开始时间:"+startT+ "结束时间:"+endT+"共 "+day+ "天 准确时间是:小时= "+hor+ " 分钟 "+secd );
return day;
}
Date date = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
try {
date = inputFormat.parse(inVal);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
}
public String fromLongToDate(long inVal) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date currentTime = new Date(inVal);
return sdf.format(currentTime);
}
public int setDate(){
String str="";
DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dt=new Date();
str=format1.format(dt);
long startT=fromDateStringToLong(str);
long endT=fromDateStringToLong("2011-01-27 00:00:00");//活动开始时间
// long fin=df.fromDateStringToLong( "1970-01-01 0:00 ");
// System.out.println(startT+ "== "+endT+ "== "+(startT-endT)+ "== "+fin);
long mint=(startT-endT)/(1000);
int hor=(int)mint/3600;
int secd=(int)mint%3600;
int day=(int)hor/24;
System.out.println("开始时间:"+startT+ "结束时间:"+endT+"共 "+day+ "天 准确时间是:小时= "+hor+ " 分钟 "+secd );
return day;
}