public static boolean checkDate(String date1, String date2) { Date date11 = DateUtil.str2Date(date1, "yyyy-MM-dd HH:mm:ss"); // 起始时间 Date date22 = DateUtil.str2Date(date2, "yyyy-MM-dd HH:mm:ss"); // 终止时间 Calendar scalendar = Calendar.getInstance(); scalendar.setTime(date11); // 起始时间 Calendar ecalendar = Calendar.getInstance(); ecalendar.setTime(date22); // 终止时间 Calendar calendarnow = Calendar.getInstance(); System.out.println(date11.toString()); System.out.println(date22.toString()); System.out.println(scalendar.toString()); System.out.println(ecalendar.toString()); System.out.println(calendarnow.toString()); if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) { return true; } else { return false; } }
/** * 比较时间是否在这两个时间点之间 * * @param time1 * @param time2 * @return */ public static boolean checkTime(String time1, String time2) { Calendar calendar = Calendar.getInstance(); Date date1 = calendar.getTime(); Date date11 = DateUtil.str2Date(DateUtil.date2Str(date1, "yyyy-MM-dd") + " " + time1); // 起始时间 Calendar c = Calendar.getInstance(); c.add(Calendar.DATE, 1); Date date2 = c.getTime(); Date date22 = DateUtil.str2Date(DateUtil.date2Str(date2, "yyyy-MM-dd") + " " + time2); // 终止时间 Calendar scalendar = Calendar.getInstance(); scalendar.setTime(date11); // 起始时间 Calendar ecalendar = Calendar.getInstance(); ecalendar.setTime(date22); // 终止时间 Calendar calendarnow = Calendar.getInstance(); if (calendarnow.after(scalendar) && calendarnow.before(ecalendar)) { return true; } else { return false; } }
public static void main(String arf[]) { String time1 = "2009-05-07 19:20:00"; String time2 = "2009-05-08 19:30:00"; DateUtil d = new DateUtil(); // System.out.println(d.checkDate(time1, time2)); // System.out.println(d.date2Str(new Date())); // System.out.println(d.checkOnly6Month("2009-03-01", -5)); Date date = DateUtil.str2Date("2010-11-22 22:32:39", "yyyy-MM-dd HH:mm:ss"); // String str = "2010-12-10 11:40:00.0"; // if(str.contains(".")){ // str = str.substring(0, str.indexOf('.')); // } String str = date2Str(new Date(), "yyyyMM"); System.out.println(str); System.out.println(date); }
/** * 检查输入日期是否是interval月之内的日期 * * @param time * @param interval * @return */ public static boolean checkOnly6Month(String time, int interval) { boolean t = true; Calendar calendarnow = Calendar.getInstance(); Date datetmp = DateUtil.str2Date(time + " 00:00:01"); Calendar scalendar = Calendar.getInstance(); scalendar.setTime(datetmp); // 要判断的时间点 calendarnow.add(Calendar.MONTH, interval); // 将当前日期前翻interval个月 calendarnow.set(Calendar.DAY_OF_MONTH, 1); // 将当前日期前翻interval个月之后 // 将日期翻到该月第一天 calendarnow.set(Calendar.HOUR_OF_DAY, 0); calendarnow.set(Calendar.MINUTE, 0); calendarnow.set(Calendar.SECOND, 0); if (!scalendar.after(calendarnow)) { t = false; } return t; }