如题.各位有什么好的意见.?
我用了日期类这个方法
if (cal.get(Calendar.HOUR_OF_DAY) == 13
|| cal.get(Calendar.HOUR_OF_DAY) == 15
|| cal.get(Calendar.HOUR_OF_DAY) == 20
&& cal.get(Calendar.MINUTE) < 11)
就是13点,15点,20点的之前的10分钟,行吗.?
可以写一个compare方法,把时间转换成到1970年0点0分的毫秒数,在做比较
不知道java有timeinterval 这个类没有,有的话可以比较算出三个时间的timeinterval然后判断
不行啊。
要将分钟的比较条件放在第一位,否则会变成每个13,15和20点和每个小时的前10分钟。
感觉你的标题和你的问题貌似搭不上啊~
private static final String formatStr = "HH:mm";
private static SimpleDateFormat sdf=new SimpleDateFormat(formatStr);
public static void main(String args[]) throws ParseException {
String tS = "13:00";
String tE = "13:10";
if(isInZone(getLong(tS),getLong(tE),getCurrentTime())){
// Your Code
}
}
private static boolean isInZone(long tStart,long tEnd,long t) throws ParseException {
return tStart <= t && t <= tEnd;
}
private static long getLong(String timeStr) throws ParseException {
return sdf.parse(timeStr).getTime();
}
private static long getCurrentTime() throws ParseException {
return getLong(sdf.format(new Date()));
}
time - startTime > 0 endtime - time > 0