日期:2014-05-19 浏览次数:20734 次
String s = "2012-08-13 08:00:00"; String[] arr = new String[] { "2012-08-13 03:50:00", "2012-08-13 08:50:00", "2012-08-13 21:50:00" }; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long d = sdf.parse(s).getTime(); for (String a : arr) { long t = sdf.parse(a).getTime(); long c = (t - d) / 1000 / 60 / 60; if (c >= 0 && c / 12 % 3 == 0 || c < 0 && c / 12 % 3 != 0) { System.out.println(a + " : 上班"); } else { System.out.println(a + " : 休息"); } }
------解决方案--------------------
public static boolean isWorking( String checkStr, String baseStr, long workTime, long restTime) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); long base = sdf.parse(baseStr).getTime(); long target = sdf.parse(checkStr).getTime(); long mod = (target-base) % (workTime+restTime); if (mod < 0) mod += (workTime+restTime); return mod < workTime; } //test String base = "2012-08-13 08:00:00"; String[] date = {"2012-08-13 03:50:00", "2012-08-13 08:50:00", "2012-08-13 21:50:00"}; for (String s : date) { if (isWorking(s, base, 12*60*60*1000, 24*60*60*1000)) { System.out.printf("%s: work\n", s); } else { System.out.printf("%s: rest\n", s); } }
------解决方案--------------------
如果用 6 楼的建议的话,是:
int segment = (int) ((hour / 24) % 3); // 计算出所属时段,0则说明是上班时间。
此外,你的上班时间,应该精确到小时,而非天;因为8月14日8点以前是上班时间而8点以后则不是。