/** * @return the time1 */ public int getTime1() { return time1; } /** * @param time1 the time1 to set */ public void setTime1(int time1) { this.time1 = time1; } /** * @return the time2 */ public int getTime2() { return time2; } /** * @param time2 the time2 to set */ public void setTime2(int time2) { this.time2 = time2; } /** * @return the capable */ public boolean isCapable() { return capable; } /** * @param capable the capable to set */ public void setCapable(boolean capable) { this.capable = capable; } /** * 检测给定时间的有效性 * * @param now * @return */ public boolean check(Date now) { boolean result = false;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;/**
* 时间段有效性监测
* 一个实例针对单个规则
* @author Sam
*
*/
public class TimeChecker
{
/** 时刻1 */
private int time1;
/** 时刻2 */
private int time2;
/** 有效性 */
private boolean capable;
/**
* 构造子
*
* 2个时刻均精确到小时,按照从小到大的顺序排列,capable为指定这二者之间的有效性
* 若需要跨越半夜0点,则需反向设置
*
* @param time1 时刻1
* @param time2 时刻2
* @param capable 有效性
*/
public TimeChecker(int time1, int time2, boolean capable)
{
super();
this.time1 = time1;
this.time2 = time2;
this.capable = capable;
init();
}
/**
* 调整时刻关系,time1始终为小
*/
private void init()
{
if (time1 > time2)
{
time1^=time2;
time2^=time1;
time1^=time2;
}
}
/**
* @return the time1
*/
public int getTime1()
{
return time1;
} /**
* @param time1 the time1 to set
*/
public void setTime1(int time1)
{
this.time1 = time1;
} /**
* @return the time2
*/
public int getTime2()
{
return time2;
} /**
* @param time2 the time2 to set
*/
public void setTime2(int time2)
{
this.time2 = time2;
} /**
* @return the capable
*/
public boolean isCapable()
{
return capable;
} /**
* @param capable the capable to set
*/
public void setCapable(boolean capable)
{
this.capable = capable;
} /**
* 检测给定时间的有效性
*
* @param now
* @return
*/
public boolean check(Date now)
{
boolean result = false;
// 以给定日期对象构造日历
Calendar calendar = Calendar.getInstance();
calendar.setTime(now);
// 获取给定日期的小时数,并判定是否在范围之内
int hour = calendar.get(Calendar.HOUR_OF_DAY);
if (hour >= time1 && hour <= time2)
{
result = true;
}
// 如果为反向判定,则将结果置反
if (!capable)
{
result = !result;
}
return result;
} /**
* 测试方法
*
* @param args
* @throws ParseException
*/
public static void main(String[] args) throws ParseException
{
TimeChecker tc05_20 = new TimeChecker(5, 20, true); // 正向判定
TimeChecker tc22_04 = new TimeChecker(22, 4, false); // 反向判定
// 根据指定格式构造2个时间
String formatStr = "yyyyMMdd hh:mm";
DateFormat formater = new SimpleDateFormat(formatStr);
String dataStr1 = "20110401 10:00";
String dataStr2 = "20110401 01:00";
String dataStr3 = "20110401 21:00";
// 解析出日期对象
Date date10 = formater.parse(dataStr1);
Date date01 = formater.parse(dataStr2);
Date date21 = formater.parse(dataStr3);
// 测试开始
System.out.println(tc05_20.check(date10));
System.out.println(tc05_20.check(date01));
System.out.println(tc05_20.check(date21));
System.out.println(tc22_04.check(date10));
System.out.println(tc22_04.check(date01));
System.out.println(tc22_04.check(date21));
// 复合测试
System.out.println(tc05_20.check(date10) || tc22_04.check(date10));
System.out.println(tc05_20.check(date01) || tc22_04.check(date01));
System.out.println(tc05_20.check(date21) || tc22_04.check(date21));
}
}
还可以另外做一个类,去将若干TimeChecker 的实例串到一起,可以完成更复杂的校验
java 比较时间大小 String s1=”2008-01-25 09:12:09″;
String s2=”2008-01-29 09:12:11″;
java.text.DateFormat df=new java.text.SimpleDateFormat(”yyyy-MM-dd HH:mm:ss”);
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
try
{
c1.setTime(df.parse(s1));
c2.setTime(df.parse(s2));
}catch(java.text.ParseException e){
System.err.println(”格式不正确”);
}
int result=c1.compareTo(c2);
if(result==0)
System.out.println(”c1相等c2″);
else if(result<0)
System.out.println(”c1小于c2″);
else
System.out.println(”c1大于c2″);
看看这个吧