后台数据库定义的日期类型是Long,即以时间戳的形式存储;页面上搜索框的日期类型则为String类型。而日期的String和Long类型在时间戳上并不相等!
我在代码中展现怎么转换日期类型,可以通过debug和时间戳的转换器来理解两者的区别。
1、首先在页面搜索框(仅是搜索框,其他展示和增删改查定义为Long),为了区分,Long类型的日期默认设置为beginTime、endTime,String类型的日期设置为beginTimeString、endTimeString
2、后台数据库和Mapper.xml查询条件的日期类型
3、在queryReq.java类中定义四个数据类型,进行转换(该类接受搜索框传递的参数)
自定义日期转换方法(部分代码,此处可用的看前五个方法):
1 /** 2 * String格式时间转换我UnixTime,如2014-8-12 转换结果不包括毫秒 3 */ 4 public static long convertDateToTimestamp(String datetime, String format) 5 throws ParseException { 6 return getFormatter(format.trim()).parse(datetime.trim()).getTime() / 1000; 7 } 8 9 /** 10 * String格式时间转换我UnixTime,起始时间为00:00:00 11 */ 12 public static long convertStartDateToTimestamp(String datetime, 13 String format) throws ParseException { 14 datetime = datetime + " 00:00:00"; 15 return getFormatter(format.trim()).parse(datetime.trim()).getTime() / 1000; 16 } 17 18 /** 19 * String格式时间转换我UnixTime,起始时间为00:00:00 20 */ 21 public static Long convertStartDateToTimestamp(String datetime) { 22 datetime = datetime + " 00:00:00"; 23 try { 24 return getFormatter("yyyy-MM-dd HH:mm:ss").parse(datetime.trim()).getTime() / 1000; 25 } catch (ParseException e) { 26 logger.error("ERROR:", e); 27 return null; 28 } 29 30 } 31 32 /** 33 * String格式时间转换我UnixTime,结束时间为23:59:59 34 */ 35 public static long convertEndDateToTimestamp(String datetime, String format) 36 throws ParseException { 37 datetime = datetime + " 23:59:59"; 38 return getFormatter(format.trim()).parse(datetime.trim()).getTime() / 1000; 39 } 40 41 /** 42 * String格式时间转换我UnixTime,结束时间为23:59:59 43 */ 44 public static Long convertEndDateToTimestamp(String datetime) { 45 46 datetime = datetime + " 23:59:59"; 47 try { 48 return getFormatter("yyyy-MM-dd HH:mm:ss").parse(datetime.trim()).getTime() / 1000; 49 } catch (ParseException e) { 50 logger.error("ERROR:", e); 51 return null; 52 } 53 } 54 55 /** 56 * Date转时间戳 转换结果不包括毫秒 57 */ 58 public static long convertDateToTimestamp(Date datetime) 59 throws ParseException { 60 // SimpleDateFormat formater=getFormatter(format.trim()); 61 // return formater.parse(formater.format(datetime)).getTime(); 62 return datetime.getTime() / 1000; 63 } 64 65 /* 66 * String格式时间转换我UnixTime,如2014-8-12 此方法暂不使用 67 */ 68 @SuppressWarnings("unused") 69 private static long convertDateToLong(String datetime, String format) 70 throws ParseException { 71 return getFormatter(format.trim()).parse(datetime.trim()).getTime(); 72 } 73 74 /* 75 * Date转时间戳 此方法暂不使用 76 */ 77 @SuppressWarnings("unused") 78 private static long convertDateToLong(Date datetime) throws ParseException { 79 // SimpleDateFormat formater=getFormatter(format.trim()); 80 // return formater.parse(formater.format(datetime)).getTime(); 81 return datetime.getTime(); 82 } 83 84 /* 85 * UnixTime转换为String 86 */ 87 public static String convertTimestampToDate(long time, String format) { 88 return getFormatter(format.trim()).format(new Date(time * 1000)).trim(); 89 } 90 91 /* 92 * UnixTime转换为Date 93 */ 94 public static Date convertTimestampToDate(long time) { 95 // SimpleDateFormat formater=getFormatter(format.trim()); 96 // Date date=null; 97 // try { 98 // date= formater.parse(formater.format(time)); 99 // } catch (ParseException e) { 100 // e.printStackTrace(); 101 // } 102 return new Date(time * 1000); 103 } 104 105 /* 106 * UnixTime转换为String 107 */ 108 public static String convertLongToDate(long time, String format) { 109 return getFormatter(format.trim()).format(new Date(time)).trim(); 110 } 111 112 /* 113 * UnixTime转换为String 114 */ 115 public static String convertDateToDateString(Date date, String format) { 116 return getFormatter(format.trim()).format(date).trim(); 117 } 118 119 /* 120 * UnixTime转换为Date 121 */ 122 @SuppressWarnings("unused") 123 private static Date convertLongToDate(long time) { 124 // SimpleDateFormat formater=getFormatter(format.trim()); 125 // Date date=null; 126 // try { 127 // date= formater.parse(formater.format(time)); 128 // } catch (ParseException e) { 129 // e.printStackTrace(); 130 // } 131 return new Date(time); 132 } 133 134 /* 135 * String 转换 Date 136 */ 137 public static Date ConvertDate(String time, String format) { 138 try { 139 SimpleDateFormat formater = getFormatter(format.trim()); 140 return formater.parse(time); 141 } catch (ParseException e) { 142 logger.error("ERROR:", e); 143 return null; 144 } 145 } 146 147 /** 148 * 取得当前时间的时间戳 不带毫秒 149 * 150 * @return 时间戳 151 */ 152 public static long currentTimestamp() { 153 return System.currentTimeMillis() / 1000; 154 } 155 156 public static long getTodayDateOfTimestamp() { 157 long t = System.currentTimeMillis() / 1000 / 60 / 60 / 24; 158 return t * 24 * 3600; 159 160 } 161 162 // public static long getNextWeekDateOfLong() { 163 // calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 164 // calendar.add(Calendar.WEEK_OF_YEAR, 1); 165 // Date d = calendar.getTime(); 166 // return calendar.getTimeInMillis() / 1000; 167 // 168 // } 169 170 public static long getNextMonthDateOfTimestamp(long thisMonth) { 171 Calendar calendar = Calendar.getInstance(); 172 Date thisMonthDate = new Date(thisMonth * 1000); 173 calendar.setTime(thisMonthDate); 174 calendar.add(Calendar.MONTH, 1); 175 // Date d = calendar.getTime(); 176 return calendar.getTimeInMillis() / 1000; 177 178 } 179 180 public static long getNextIntervalTimeOfTimestamp(long thisDay, int days) { 181 Calendar calendar = Calendar.getInstance(); 182 Date thisDayDate = new Date(thisDay * 1000); 183 calendar.setTime(thisDayDate); 184 calendar.add(Calendar.DATE, days); 185 calendar.set(Calendar.HOUR_OF_DAY, 24); 186 calendar.set(Calendar.SECOND, 0); 187 calendar.set(Calendar.MINUTE, 0); 188 calendar.set(Calendar.MILLISECOND, 0); 189 return calendar.getTimeInMillis() / 1000; 190 191 } 192 193 public static long getSpecifiedMonthDateOfTimestamp(long thisMonth, 194 int amount) { 195 Calendar calendar = Calendar.getInstance(); 196 Date thisMonthDate = new Date(thisMonth * 1000); 197 calendar.setTime(thisMonthDate); 198 calendar.add(Calendar.MONTH, amount); 199 // Date d = calendar.getTime(); 200 return calendar.getTimeInMillis() / 1000; 201 202 } 203 204 public static long getLastDayOfThisYear() { 205 Calendar calendar = Calendar.getInstance(); 206 Date d = new Date(); 207 calendar.setTime(d); 208 209 calendar.set(calendar.get(Calendar.YEAR), 12, 31, 23, 59, 59); 210 return calendar.getTimeInMillis() / 1000; 211 } 212 213 public static int getCurrentMonth() { 214 Calendar calendar = Calendar.getInstance(); 215 return calendar.get(Calendar.MONTH); 216 }
4、之后在service实现类中获取数据实现查询:
5、页面显示结果