请问:
如何判断用户输入的一个时间是在
上一个月的当前日到当前月的当前日之间?
如:当前时间=2009-6-15
那么用户输入的时间必需在2009-5-15到2009-6-15之间
必需考虑到跨年,跨月.
如何判断用户输入的一个时间是在
上一个月的当前日到当前月的当前日之间?
如:当前时间=2009-6-15
那么用户输入的时间必需在2009-5-15到2009-6-15之间
必需考虑到跨年,跨月.
调试欢乐多
你格式化过为string型的话是不行的。
//first="20080501";
//second="20080515";
public int nDaysBetweenTwoDate(String first, String second) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Date firstDate = null;
Date secondDate = null;
int nDay = 0;
try {
firstDate = df.parse(first);
secondDate = df.parse(second);
nDay = (int) ((secondDate.getTime() - firstDate.getTime()) / (24 * 60 * 60 * 1000));
} catch (Exception e) {
e.printStackTrace();
}
return nDay;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="" />
<meta name="keywords" content="" />
<meta name="description" content="" />
</head> <body>
<script type="text/javascript">
<!--
function dateDiff(sDate1,sDate2){//sDate1和sDate2是年-月-日格式
var aDate,oDate1,oDate2,iDays;
aDate=sDate1.split("-");
oDate1=new Date(aDate[1]+'-'+aDate[2]+'-'+aDate[0]);//转换为月-日-年格式
if(sDate2){
aDate=sDate2.split("-");
oDate2=new Date(aDate[1] + '-'+aDate[2]+'-'+aDate[0]);
}
else{
oDate2=new Date();
} iDays=parseInt(Math.abs(oDate1-oDate2)/1000/60/60/24); //把相差的毫秒数转换为天数
alert(oDate1+" 和 "+oDate2+" 相差 "+iDays+" 天");
}
//-->
</script>
<input type="text" onblur="dateDiff(this.value)" />
</body>
</html>
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.set(Calendar.MONTH, c.get(Calendar.MONTH)-1);
c.set(Calendar.HOUR, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
Date d1 = c.getTime();
c.set(Calendar.MONTH, c.get(Calendar.MONTH)+2);
Date d2 = c.getTime();
Date d = (new SimpleDateFormat("yyyy-MM-dd")).parse("2009-7-17");
if(d.before(d1)||d.after(d2))
System.out.println("不在范围里");
else
System.out.println("在范围里");