开始时间和结束时间都是可以手工随意设定的(范围0到24内的整数),
比如开始时间晚上10点,结束时间早上8点。
要求判断系统当前时间是否在设定时间范围内。比如:
int start=22;
int end=8;
Carlendar now=Carlendar.getInstance();
因为开始结束时间有很多种关系,弄了半天了还没搞定,
不知道大家有没有遇到过类似问题。
比如开始时间晚上10点,结束时间早上8点。
要求判断系统当前时间是否在设定时间范围内。比如:
int start=22;
int end=8;
Carlendar now=Carlendar.getInstance();
因为开始结束时间有很多种关系,弄了半天了还没搞定,
不知道大家有没有遇到过类似问题。
楼主【sunlm78】截止到2008-07-11 13:28:22的历史汇总数据(不包括此帖):
发帖的总数量:2 发帖的总分数:60 每贴平均分数:30
回帖的总数量:4 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:1 结贴的总分数:50
无满意结贴数:0 无满意结贴分:0
未结的帖子数:1 未结的总分数:10
结贴的百分比:50.00 % 结分的百分比:83.33 %
无满意结贴率:0.00 % 无满意结分率:0.00 %
楼主加油
Calendar now=Calendar.getInstance();
小时: now.get(Calendar.HOUR_OF_DAY);
分钟: now.get(Calendar.MINUTE);
直接作比较不是很麻烦啊
测试此日期是否在指定日期之后。
boolean Date.before(Date when)
测试此日期是否在指定日期之前。
你看一下API
我也知道Date类有比较方法的。但是得先把开始结束时间转换成日期类型才能比较啊
问题就在这里,只有小时设定怎么转换呢,今天还是明天?给大家看看我现在的代码吧,是有bug的
public static boolean isServiceOut() {
boolean rtn = false; int nBegin = PropertiesUtils.getInstance().getServiceOutBegin();
int nEnd = PropertiesUtils.getInstance().getServiceOutEnd(); Calendar cNow = Calendar.getInstance();
Calendar cBegin = Calendar.getInstance();
Calendar cEnd = Calendar.getInstance(); cBegin.set(Calendar.HOUR_OF_DAY, nBegin); //24H
cBegin.set(Calendar.MINUTE, 0);
cBegin.set(Calendar.SECOND, 0);
cBegin.set(Calendar.MILLISECOND, 0);
cEnd.set(Calendar.HOUR_OF_DAY, nEnd); //24H
cEnd.set(Calendar.MINUTE, 0);
cEnd.set(Calendar.SECOND, 0);
cEnd.set(Calendar.MILLISECOND, 0);
//終わり時間<開始時間の場合、終わり時間を翌日にする
if (nEnd < nBegin) {
cEnd.add(Calendar.DAY_OF_MONTH, 1);
} if (cNow.get(Calendar.HOUR_OF_DAY) < 12) {
cNow.add(Calendar.DAY_OF_MONTH, 1);
}
Date now = cNow.getTime();
Date begin = cBegin.getTime();
Date end = cEnd.getTime();
//時間帯判定
if (now.compareTo(begin) > 0 && now.compareTo(end) < 0) {
rtn = true;
}
return rtn;
}
* 判断给定时间在否在给定两个时间之前
*/
public static void do7(String star,String end){
SimpleDateFormat localTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date sdate = localTime.parse(star);
Date edate=localTime.parse(end);
long time = System.currentTimeMillis();
System.out.println(time+"##"+sdate.getTime()+"##"+edate.getTime());
if(time>=sdate.getTime()&& time<=edate.getTime()){
System.out.println("true");
}
}catch(Exception e){}
}
呵呵
now.get(Calendar.HOUR_OF_DAY);
只要这个数大于8小于22就不满足条件
SimpleDateFormat localTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date sdate = localTime.parse(star);
Date edate=localTime.parse(end);
long time = System.currentTimeMillis();
Date date=new Date();
System.out.println(time+"##"+sdate.getTime()+"##"+edate.getTime());
if(date.after(sdate)&& date.before(edate)){
System.out.println("true");
}
}catch(Exception e){}
}
SimpleDateFormat localTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date sdate = localTime.parse(star);
Date edate=localTime.parse(end);
Date date=new Date();
System.out.println(sdate.getTime()+"##"+sdate.getTime()+"##"+edate.getTime());
if(date.after(sdate)&& date.before(edate)){
System.out.println("true");
}
}catch(Exception e){}
}
你的解决方案里面参数跟我的不一样,我的入力参数只有小时,范围0到24的整数to:lovingprince
式样没有问题,开始结束时间都必须是范围0到24的整数。
》问题是你说开始时间是22点,结束时间是8点,我想问的是你开始的时间22点从当天算起,还是昨天晚上的22点算起。
很明显要看系统当前时间,如果现在是晚上23点,那么开始时间就是当天22点,如果现在是早上6点,那么开始时间就是昨天晚上22点。