首页 > 代码库 > 前端传一个时间,在后台中比较时间大小的操作
前端传一个时间,在后台中比较时间大小的操作
public void getOperatingImcomeDataList(){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date state=DateUtil.getDateByString(beginTime,DateUtil.DATE_PATTERN_23); Date edate=DateUtil.getDateByString(endTime,DateUtil.DATE_PATTERN_15); String Sdate = format.format(state); String Edate = format.format(edate); try { if(state.getTime()<edate.getTime()){ StringBuffer sb=new StringBuffer(); sb.append("select CONVERT(CHAR(7),OPERATING_DATE,120) as time," + "COUNT(LINE_ID) as totalLine, " + "SUM(TOTAL_NUMBER)as totalAmount," + "SUM(COIN_NUMBER)as totalCoin, " + "SUM(CARD_NUMBER)as totalCard," + "SUM(STUDENT_CARD_NUMBER)as totalStu, " + "SUM(ELDERLY_CARD_NUMBER)as totalEld," + "SUM(MILITARY_CARD_NUMBER)as totalMilit, " + "SUM(DISABILITIES_CARD_NUMBER)as totalDisab " + "from LN_BUS_OPERATING_INCOME where "); sb.append("OPERATING_DATE>=‘"+Sdate+"‘ and OPERATING_DATE<=‘"+Edate+"‘ "); sb.append(" GROUP BY CONVERT(CHAR(7),OPERATING_DATE,120)"); System.out.println("==============="+sb.toString()); List<Map<String, Object>> list = lnBusOperatingIncomeService.findListMapBySql(sb.toString()); writeJson(list); }else{ this.writeJson(obj.put("3", "查询结束时间不能早于开始时间!")); } } catch (Exception e){ e.printStackTrace(); } }
package com.inetsoft.hfgj.util; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import java.util.HashSet; import java.util.Locale; import java.util.Set; import java.util.TimeZone; /** * 日期处理类 * * @author dyhu * @version 1.0 * @since 2010-8-27 */ public class DateUtil { public static final int ONE_SECOND = 1000; public static final int ONE_MINUTE = 60 * ONE_SECOND; public static final int ONE_HOUR = 60 * ONE_MINUTE; public static final long ONE_DAY = 24 * ONE_HOUR; public static final long ONE_WEEK = 7 * ONE_DAY; /** * @see TimeZone#getRawOffset */ @SuppressWarnings("unused") private static final int offSet; public static final String DATE_PATTERN_01 = "yyyy/MM/dd"; public static final String DATE_PATTERN_02 = "yyyyMMddHHmmssSSS"; public static final String DATE_PATTERN_03 = "yyyyMMddHHmmss"; public static final String DATE_PATTERN_04 = "yyyy/MM/dd HH:mm:ss"; public static final String DATE_PATTERN_05 = "HH:mm:ss"; public static final String DATE_PATTERN_06 = "HH:mm"; public static final String DATE_PATTERN_07 = "HHmm"; public static final String DATE_PATTERN_08 = "yyyy/MM"; public static final String DATE_PATTERN_09 = "yyyyMM"; public static final String DATE_PATTERN_10 = "yyyy"; public static final String DATE_PATTERN_11 = "MM"; public static final String DATE_PATTERN_12 = "dd"; public static final String DATE_PATTERN_13 = "MM/dd"; public static final String DATE_PATTERN_14 = "yyyy/MM/dd HH:mm"; public static final String DATE_PATTERN_15 = "yyyy-MM-dd"; public static final String DATE_PATTERN_16 = "yyyyMMdd"; public static final String DATE_PATTERN_17 = "yyyy-MM-dd HH:mm"; public static final String DATE_PATTERN_18 = "yyyy-MM-dd HH:mm:ss"; public static final String DATE_PATTERN_19 = "yyyy年MM月dd日"; public static final String DATE_PATTERN_20 = "HHmmssS"; public static final String DATE_PATTERN_21 = "yyyyMMddHH"; public static final String DATE_PATTERN_22 = "yyyy.MM.dd HH.mm.ss"; public static final String DATE_PATTERN_23 = "yyyy-MM"; private static Set<String> datePatternSet; static { datePatternSet = new HashSet<String>(); datePatternSet.add(DATE_PATTERN_01); datePatternSet.add(DATE_PATTERN_02); datePatternSet.add(DATE_PATTERN_03); datePatternSet.add(DATE_PATTERN_04); datePatternSet.add(DATE_PATTERN_05); datePatternSet.add(DATE_PATTERN_06); datePatternSet.add(DATE_PATTERN_07); datePatternSet.add(DATE_PATTERN_08); datePatternSet.add(DATE_PATTERN_09); datePatternSet.add(DATE_PATTERN_10); datePatternSet.add(DATE_PATTERN_11); datePatternSet.add(DATE_PATTERN_12); datePatternSet.add(DATE_PATTERN_13); datePatternSet.add(DATE_PATTERN_14); datePatternSet.add(DATE_PATTERN_15); datePatternSet.add(DATE_PATTERN_16); datePatternSet.add(DATE_PATTERN_17); datePatternSet.add(DATE_PATTERN_18); datePatternSet.add(DATE_PATTERN_19); datePatternSet.add(DATE_PATTERN_20); datePatternSet.add(DATE_PATTERN_21); datePatternSet.add(DATE_PATTERN_22); datePatternSet.add(DATE_PATTERN_23); TimeZone timeZone = TimeZone.getDefault(); offSet = timeZone.getRawOffset(); } /** * 根据日期取得日期的String * @param pDate * @param datePattern * @return */ public static String getDateString(Date pDate, String datePattern) { if (StringUtil.isEmpty(datePattern) || !datePatternSet.contains(datePattern)) { // throw new IllegalArgumentException("Date Pattern error!"); return ""; } if (pDate == null) { // throw new IllegalArgumentException("inputed Date error!"); return ""; } SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern); return dateFormat.format(pDate); } /** * String to date * * @author 7308905 */ public static Date getDateByString(String str, String datePattern) { if (StringUtil.isEmpty(datePattern) || !datePatternSet.contains(datePattern)) { return null; } if (StringUtil.isEmpty(str)) { return null; } SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern); try { return dateFormat.parse(str); } catch (ParseException pe) { pe.printStackTrace(); } return null; } /** * 取得日期的星期(星期三) * @param date * @return */ static public String getXinqi(Date date) { String youbi = ""; if (date == null) { return youbi; } SimpleDateFormat formatter = new SimpleDateFormat("E"); youbi = formatter.format(date); return youbi; } /** * 取得日期的星期, * 星期一:1 * 星期二:2 * 星期三:3 * 星期四:4 * 星期五:5 * 星期六:6 * 星期日:7 * @param date * @return */ public static int getXinqiInt(Date date) { int theWeek = 0; if (date == null) { return theWeek; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); theWeek = calendar.get(Calendar.DAY_OF_WEEK); if (theWeek != 1) { theWeek = theWeek -1; } else { theWeek = 7; } return theWeek; } /** * 取得系统时间 * * @return 系统时间 */ public static Date getSysDate() { return new Date(System.currentTimeMillis()); } /** * 取得系统时间YYYYMMDD格式 * * @return 系统时间 */ public static Date getSysDateYYYYMMDD() { Date today = getSysDate(); String strToday = dt2Str(today); today = str2Dt(strToday); return today; } /** * * @return */ public static Date getDateNoTime(Date dt) { // String strToday = dt2Str(dt); // Date newDt = str2Dt(strToday); // return newDt; if (dt == null) { return null; } Calendar cl = Calendar.getInstance(); cl.setTimeInMillis(dt.getTime()); cl.set(Calendar.HOUR_OF_DAY, 0); cl.set(Calendar.MINUTE, 0); cl.set(Calendar.SECOND, 0); cl.set(Calendar.MILLISECOND, 0); return cl.getTime(); } /** * 取得yyyyMMddHHmmssSSS格式的日期字符串 * * @param dt 日期 * @return 日期字符串 */ public static String dateTime2Str(Date dt) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); return sdf.format(dt); } /** * 取得yyyy/MM/dd格式的日期字符串 * * @param dt 日期 * @return 日期字符串(yyyy/MM/dd) */ public static String dt2Str(Date dt) { if (dt != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); return sdf.format(dt); } else { return null; } } /** * 通过yyyy/MM/dd格式的日期字符串,取得日期 * * @param ymd 日期字符串 * @return 日期 */ public static Date str2Dt(String ymd) { if (ymd == null) { return null; } SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); try { Date ret = sdf.parse(ymd); return ret; } catch (ParseException e) { return null; } } /** * 取得yyyyMMdd格式的日期字符串 * * @param dt 日期 * @return 日期字符串(yyyyMMdd) */ public static String dt2Str8(Date dt) { if (dt != null) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); return sdf.format(dt); } else { return null; } } /** * 取得HH:mm格式的日期字符串 * * @param dt 日期 * @return 日期字符串(HH:mm) */ public static String dt2HHmm(Date dt) { if (dt != null) { String date = dateTime2Str(dt); return date.substring(8, 10) + ":" + date.substring(10, 12); } else { return null; } } /** * (HHmm) to (HH:mm) */ public static String dt2HHmm(String dt) { if (dt != null) { return dt.substring(0, 2) + ":" + dt.substring(2, 4); } else { return null; } } /** * */ public static Date add(Date date, int field, int amount) { if (date == null) { return null; } Calendar calendar = GregorianCalendar.getInstance(Locale.CHINA); calendar.setTime(date); calendar.add(field, amount); return calendar.getTime(); } /** * 加N个月 */ public static Date addMonths(Date date, int monthAmount) { return add(date, Calendar.MONTH, monthAmount); } /** * 加N天 */ public static Date addDays(Date date, int dayAmount) { return add(date, Calendar.DATE, dayAmount); } /** * 加N个小时 */ public static Date addHours24(Date date, int hourAmount) { return add(date, Calendar.HOUR_OF_DAY, hourAmount); } /** * 加N分钟 */ public static Date addMinutes(Date date, int minuteAmount) { return add(date, Calendar.MINUTE, minuteAmount); } /** * 加N秒 */ public static Date addSeconds(Date date, int secondAmount) { return add(date, Calendar.SECOND, secondAmount); } /** * 两个日期是否在一个月内 * @param dateFrom 开始时间 * @param dateTo 结束时间 * @param monthAmount * @return */ public static boolean isScopeOfMonth(Date dateFrom, Date dateTo, int monthAmount) { if (!dateTo.before(DateUtil.addMonths(dateFrom, monthAmount))) { return false; } return true; } /** * 获取时间(YYYY年MM月DD日 星期XX) * * @return */ public static String getSysDateWeek() { int theYear = 0; int theMonth = 0; int theDay = 0; int theWeek = 0; String theWeekUpper = ""; String thedate = ""; Calendar calendar = Calendar.getInstance(); theYear = calendar.get(Calendar.YEAR); theMonth = calendar.get(Calendar.MONTH) + 1; theDay = calendar.get(Calendar.DAY_OF_MONTH); theWeek = calendar.get(Calendar.DAY_OF_WEEK); switch (theWeek) { case 1: theWeekUpper = "日"; break; case 2: theWeekUpper = "一"; break; case 3: theWeekUpper = "二"; break; case 4: theWeekUpper = "三"; break; case 5: theWeekUpper = "四"; break; case 6: theWeekUpper = "五"; break; case 7: theWeekUpper = "六"; break; default: break; } thedate = "" + theYear + "年" + theMonth + "月" + theDay + "日" + " 星期" + theWeekUpper; return thedate; } /** * 日期显示 * @param b * @return */ public static String getDateWeek(Date b) { // int theYear = 0; int theMonth = 0; int theDay = 0; int theWeek = 0; String theWeekUpper = ""; String thedate = ""; Calendar calendar = Calendar.getInstance(); calendar.setTime(b); // theYear = calendar.get(Calendar.YEAR); theMonth = calendar.get(Calendar.MONTH) + 1; String strMonth = StringUtil.addZero(String.valueOf(theMonth), 2); theDay = calendar.get(Calendar.DAY_OF_MONTH); String strDay = StringUtil.addZero(String.valueOf(theDay), 2); theWeek = calendar.get(Calendar.DAY_OF_WEEK); switch (theWeek) { case Calendar.SUNDAY: theWeekUpper = "日"; break; case Calendar.MONDAY: theWeekUpper = "一"; break; case Calendar.TUESDAY: theWeekUpper = "二"; break; case Calendar.WEDNESDAY: theWeekUpper = "三"; break; case Calendar.THURSDAY: theWeekUpper = "四"; break; case Calendar.FRIDAY: theWeekUpper = "五"; break; case Calendar.SATURDAY: theWeekUpper = "六"; break; default: break; } thedate = strMonth + "月" + strDay + "日(周" + theWeekUpper+")"; return thedate; } public static Date mondayOfWeek(Date date) { Calendar cl = Calendar.getInstance(); cl.setTime(date); int dayOfWeek = cl.get(Calendar.DAY_OF_WEEK); dayOfWeek = dayOfWeek == 1 ? 6 : dayOfWeek - 2; cl.add(Calendar.DAY_OF_WEEK, -dayOfWeek); return cl.getTime(); } public static Date sundayOfWeek(Date date) { Calendar cl = Calendar.getInstance(); cl.setTime(date); int dayOfWeek = cl.get(Calendar.DAY_OF_WEEK); dayOfWeek = dayOfWeek == 1 ? 7 : dayOfWeek - 1; cl.add(Calendar.DAY_OF_WEEK, 7 - dayOfWeek); return cl.getTime(); } /** * 计算时间差 * @param fromDate 开始时间 * @param toDate 结束时间 * @return 相差几天 */ public static int datePoor(Date fromDate, Date toDate) { fromDate = DateUtil.getDateByString(DateUtil.getDateString(fromDate, DATE_PATTERN_18),DATE_PATTERN_15); toDate = DateUtil.getDateByString(DateUtil.getDateString(toDate, DATE_PATTERN_18),DATE_PATTERN_15); return (int)((toDate.getTime() - fromDate.getTime())/(1000*60*60*24)); } /** * 计算时间差 * @param fromDate 开始时间 * @param toDate 结束时间 * @return 相差多少分钟 */ public static int minPoor(Date fromDate, Date toDate) { return (int)((toDate.getTime() - fromDate.getTime())/(1000*60)); } /** * 计算时间差 * @param fromDate 开始时间 * @param toDate 结束时间 * @return 相差秒 */ public static int secondPoor(Date fromDate, Date toDate) { return (int)((toDate.getTime() - fromDate.getTime())/1000); } /** * 找到最近的周一 * * @param k * @return */ public static java.util.Date getNearMonday(java.util.Date k) { if (k != null) { return addDays(k, 0 - (getXinqiInt(k) - 1)); } else { return null; } } /** * 找到最近的周六 * * @param k * @return */ public static java.util.Date getNearSaturday(java.util.Date k) { if (k != null) { return addDays(k, 0 - (getXinqiInt(k) + 1)); } else { return null; } } /** * 如 1415948324 转成 2014-11-14 14:58:44 * 将时间戳转成字符串类型 * @author aicc * @param dateString * @return */ public static String formatStringToDateString(String dateString){ String str = ""; try{ if(dateString.contains(":")){//判断传过来的时间是否是时间类型的字符串,如果是则直接返回,否则转成时间类型字符串 str = dateString; }else if(!"".equals(dateString)){ dateString = dateString+"000"; Long l = Long.valueOf(dateString); Date date = new Date(l); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); str = sdf.format(date); } }catch(Exception e){ e.printStackTrace(); } return str; } /** * 获取本周第一天(这里第一天为礼拜一,而不是礼拜天) * @author aicc * @return */ public static String getWeekFirstDateString(String datePattern){ String str = ""; str = DateUtil.getDateString(getNearMonday( new Date()),datePattern); return str; } /** * 获取上周第一天(这里第一天为礼拜一,而不是礼拜天) * @author aicc * @return */ public static String getBeforeWeekFirstDateString(String datePattern){ String str = ""; str = DateUtil.getDateString(getNearMonday(addDays(new Date(), -7)), datePattern); return str; } /** * 获取上周最后一天(这里最后一天为礼拜天,而不是礼拜六) * @return */ public static String getBeforeWeekLastDateString(String datePattern){ String str = ""; Date d = getNearMonday( new Date()); str = DateUtil.getDateString(addDays(d, -1), datePattern); return str; } public static int compareDate(String date1, String date2, String pattern) { DateFormat df = new SimpleDateFormat(pattern); try { Date d1 = df.parse(date1); Date d2 = df.parse(date2); if (d1.getTime() > d2.getTime()) { return 1; } else if (d1.getTime() < d2.getTime()) { return -1; } else { return 0; } } catch (Exception exception) { exception.printStackTrace(); } return -9999; } /** * 获取指定时间的最后一天 * @param monthString * @param pattern * @return */ public static String getLastDayOfMonth(String monthString,String pattern){ int year = Integer.parseInt(monthString.split("-")[0]); int month = Integer.parseInt(monthString.split("-")[1]); Calendar cal = Calendar.getInstance(); //设置年份 cal.set(Calendar.YEAR,year); //设置月份 cal.set(Calendar.MONTH, month-1); //获取某月最大天数 int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, lastDay); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat(pattern); String lastDayOfMonth = sdf.format(cal.getTime()); return lastDayOfMonth; } /** * 比较两个日期的大小 * @param dateStr1 格式为:yyyy-MM-dd * @param dateStr2 格式为:yyyy-MM-dd * @return 返回0:第一个大于第二个 * 返回1:两个相等 * 返回2:第一个小于第二个 */ public static String compareTwoDate(String dateStr1, String dateStr2, String formatStr) { if (dateStr1.length() == 8) { dateStr1 = transfer2LongDateDtPart(dateStr1); } if (dateStr2.length() == 8) { dateStr2 = transfer2LongDateDtPart(dateStr2); } SimpleDateFormat format = new SimpleDateFormat(formatStr); try{ Date date1 = format.parse(dateStr1); Date date2 = format.parse(dateStr2); if (date1.getTime() > date2.getTime()) { return "0"; } else if (date1.getTime() == date2.getTime()) { return "1"; } else { return "2"; } }catch(Exception e){ e.printStackTrace(); return null; } } /** * 转换日期格式 yyyyMMdd => yyyy-MM-dd * @param dt yyyyMMdd * @return yyyy-MM-dd */ public static String transfer2LongDateDtPart(String dt){ if(dt==null||dt.length()!=8){ return dt; } return dt.substring(0, 4).concat("-").concat(dt.substring(4, 6)).concat("-").concat(dt.substring(6)); } /** * DATE to String,支持多种格式 * @param date * @return */ public static String dateToString(Date date,String format){ if (date == null){ return null; } return new SimpleDateFormat(format).format(date); } /** * @author hcc * 1.比较时间大小的 * 2.(开始时间+addTime)和endTime的大小, * 3.如果(开始时间+addTime)小于endTime则返回true,否则返回false * 4.format格式必须是 HH:mm * 5.addTime必须是 以小时为单位 * 6.beginTime和endTime的格式必须是 HH:mm * @param beginTime * @param endTime * @param format * @param addTime * @return */ public static boolean compareTime(String beginTime,String endTime,String format,String addTime){ boolean flag = false; String currentDate = getDateString(new Date(), DATE_PATTERN_15); beginTime = currentDate + " " + beginTime; endTime = currentDate + " " + endTime; Date bDate = getDateByString(beginTime, DATE_PATTERN_17); Date eDate = getDateByString(endTime, DATE_PATTERN_17); //相差多少分钟 int min = minPoor(bDate,eDate); int addMin = (int) (Float.parseFloat(addTime)*60); if(addMin < min){//表示 (开始时间+addTime)小于endTime flag = true; } return flag; } /** * 获取几天的星期数组 */ public static String[] getNextDays(int i ){ String days = "星期日星期一星期二星期三星期四星期五星期六"; String[] result = new String[i]; Calendar instance = Calendar.getInstance(); for(int k =0;k<i;k++){ instance.add(Calendar.DATE,1); if(k==0){ result[k] = "明天"; }else{ int num = instance.get(Calendar.DAY_OF_WEEK); result[k] = days.substring((num-1)*3,num*3); } } return result; } /** * 获取几天的日期数组 */ public static String[] getNextDates(int i){ SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-dd"); String[] result = new String[i]; Calendar instance = Calendar.getInstance(); for(int k =0;k<i;k++){ instance.add(Calendar.DATE,1); result[k] = sdf.format(instance.getTime()); } return result; } /** * 测试方法 * @author aicc * @param args */ public static void main(String[] args){ compareTime("17:09","20:00","HH:mm","2"); } }
前端传一个时间,在后台中比较时间大小的操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。