小记Java时间工具类
小记Java时间工具类
这里主要记录以下几个工具
- 两个时间只差(Data)
- 获取时间的格式 格式化时间 返回String
- 两个时间只差(String)
- 获取两个时间之间的日期、月份、年份
- 获取给定日期之前会之后的日期
- 忽略年月日,仅比较两个时分之间的差 单位分钟
- 获取两个时间的间隔天数(忽略了时分秒) 0 则都是当天的 》1 则是跨天 计算收费专用
- 获取两个时间段内的分段集合 计费专用
- 判断两个时间区间是否有交集
以下是代码块,不足之处还望留言指正,万分感谢。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | package com.ftwj.parking.utils; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; public class DateUtils { /** * 两个时间只差 * @param startDate * @param endDate * @return 分钟 */ public static Integer getBetweenMinutes(Date startDate, Date endDate) { Integer minutes = 0 ; try { if (startDate!= null &&endDate!= null ) { long ss = 0 ; if (startDate.before(endDate)) { ss = endDate.getTime() - startDate.getTime(); } else { ss = startDate.getTime() - endDate.getTime(); } minutes = Integer.valueOf(( int ) (ss/( 60 * 1000 ))) ; } } catch (Exception e) { e.printStackTrace(); } return minutes; } /** * @Title: getDatFormat 获取时间的格式 格式化时间 返回String * @Description: * @param: @param date 日期 * @param: @param dateFormat 魔板 * @param: @return * @throws 包福平 * @return: String */ public static String getDatFormat(Date date, String dateFormat) { try { SimpleDateFormat format = new SimpleDateFormat(dateFormat); return format.format(date); } catch (Exception e) { e.printStackTrace(); SimpleDateFormat format = new SimpleDateFormat( "YYYY-MM-DD" ); return format.format( new Date()); } } /** * 两个时间只差 * @param startDate * @param endDate * @return 分钟 */ public static Integer getBetweenMinutes(String a, String b) { SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); Date startDate; Date endDate; try { startDate = format.parse(a); endDate = format.parse(b); try { long ss = 0 ; if (startDate.before(endDate)) { ss = endDate.getTime() - startDate.getTime(); } else { ss = startDate.getTime() - endDate.getTime(); } return Integer.valueOf(( int ) (ss/( 60 * 1000 ))) ; } catch (Exception e) { e.printStackTrace(); return 0 ; } } catch (ParseException e1) { e1.printStackTrace(); return 0 ; } } /** * 获取两个时间之间的日期、月份、年份 * @param beginDate * @param endDate * @param type{1:日期,2:月份,其他:年份} * @return */ public static List<String> getBetweenDates(String startDate, String endDate, Integer type) { SimpleDateFormat format= null ; Date begin = null ; Date end = null ; Integer obj = null ; Integer flag = null ; if (type!= null &&type== 1 ){ format = new SimpleDateFormat( "yyyy-MM-dd" ); obj = Calendar.DAY_OF_YEAR; flag = 9 ; } else if (type!= null &&type== 2 ){ format = new SimpleDateFormat( "yyyy-MM" ); obj = Calendar.MONTH; flag = 11 ; } else { format = new SimpleDateFormat( "yyyy" ); obj = Calendar.YEAR; flag = 9 ; } if (StringUtils.isNotEmpty(startDate)&&StringUtils.isNotEmpty(endDate)){ try { begin = format.parse(startDate); end = format.parse(endDate); } catch (ParseException e) { e.printStackTrace(); } } else { end = new Date(); begin = getDateBefore(end,flag,obj); } List<String> result = new ArrayList<String>(); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(begin); while (begin.getTime() <= end.getTime()) { result.add(format.format(tempStart.getTime())); tempStart.add(obj, 1 ); begin = tempStart.getTime(); } return result; } /** * 获取两个时间之间的日期、月份、年份 * @param date{YYYY-MM-DD-YYYY-MM-DD} * @param type{1:日期,2:月份,其他:年份} * @return */ public static List<String> getBetweenDates(String date,Integer type) { SimpleDateFormat format= null ; Date begin = null ; Date end = null ; Integer obj = null ; String startDate = null ; String endDate = null ; Integer flag = null ; if (type!= null &&type== 1 ){ format = new SimpleDateFormat( "yyyy-MM-dd" ); obj = Calendar.DAY_OF_YEAR; flag = 9 ; } else if (type!= null &&type== 2 ){ format = new SimpleDateFormat( "yyyy-MM" ); obj = Calendar.MONTH; flag = 11 ; } else { format = new SimpleDateFormat( "yyyy" ); obj = Calendar.YEAR; flag = 9 ; } if (StringUtils.isNotEmpty(date)){ startDate = date.substring( 0 , 10 ); endDate = date.substring(date.length()- 10 , date.length()); try { begin = format.parse(startDate); end = format.parse(endDate); } catch (ParseException e) { e.printStackTrace(); } } else { end = new Date(); begin = getDateBefore(end,flag,obj); } List<String> result = new ArrayList<String>(); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(begin); while (begin.getTime() <= end.getTime()) { result.add(format.format(tempStart.getTime())); tempStart.add(obj, 1 ); begin = tempStart.getTime(); } return result; } public static Date getDateBefore(Date d, int day,Integer type) { Calendar now = Calendar.getInstance(); now.setTime(d); now.set(type, now.get(type) - day); return now.getTime(); } /** * 获取给定日期之前会之后的日期 * @param date * @param type * @param num * @return */ public static String getPreviouslyDate(Date date, String type, Integer num) { SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd" ); String resultDate = "" ; Calendar c = Calendar.getInstance(); c.setTime(date); try { switch (type) { case "day" : c.add(Calendar.DATE, num); break ; case "month" : c.add(Calendar.MONTH, num); break ; case "year" : c.add(Calendar.YEAR, num); break ; default : c.add(Calendar.DATE, 0 ); break ; } Date d = c.getTime(); resultDate = format.format(d); return resultDate; } catch (Exception e) { e.printStackTrace(); return null ; } } /** * @Title: 忽略年月日,仅比较两个时分之间的差 单位分钟 * @Description: * @param: @param a * @param: @param b * @param: @return * @throws 包福平 * @return: Integer */ public static Integer changeYMDtoEqual(Date a,Date b) { if ( null ==a|| null ==b) { return null ; } String ymd=getDatFormat( new Date(), "yyyy-MM-dd" ); String tmpa=ymd+ " " +getDatFormat(a, "HH:mm:ss" ); String tmpb=ymd+ " " +getDatFormat(b, "HH:mm:ss" ); return getBetweenMinutes(tmpa, tmpb); } /** * @Title: getBetweenDay 获取两个时间的间隔天数(忽略了时分秒) 0 则都是当天的 》1 则是跨天 计算收费专用 * @Description: * @param: @param startTime * @param: @param endTime * @param: @return * @param: @throws ParseException * @throws 包福平 * @return: Integer */ public static Integer getBetweenDay(Date startDate,Date endDate) throws ParseException { // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //跨年的情况会出现问题哦 //如果时间为:2016-03-18 11:59:59 和 2016-03-19 00:00:01的话差值为 1 Calendar aCalendar = Calendar.getInstance(); aCalendar.setTime(startDate); int day1 = aCalendar.get(Calendar.DAY_OF_YEAR); aCalendar.setTime(endDate); int day2 = aCalendar.get(Calendar.DAY_OF_YEAR); int days=day2-day1; return days; } /** * @Title: getDayDetailsBetweenDates * @Description: 获取两个时间段内的分段集合 计费专用 * 例子: String startTime="2019-01-12 7:30:33"; String endTime="2019-01-14 7:30:34"; 结果 2019-01-12 07:30:33----2019-01-13 00:00:00 2019-01-13 00:00:00----2019-01-14 00:00:00 2019-01-14 00:00:00----2019-01-14 07:30:34 * @param: @param startDate * @param: @param endDate * @param: @return * @param: @throws ParseException * @throws 包福平 * @return: List<Map<String,Date>> */ public static List<Map<String,Date>> getDayDetailsBetweenDates(Date startDate,Date endDate) throws ParseException{ Integer days=getBetweenDay(startDate, endDate); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); List<Map<String,Date>> list= new ArrayList<Map<String,Date>>(); for ( int i = 0 ; i <= days; i++) { System.out.println(i); Map<String,Date> map = new HashMap<String,Date>(); if (i== 0 ) { map.put( "start" , startDate); } else { map.put( "start" , sdf.parse(getPreviouslyDate(startDate, "day" , i)+ " 00:00:00" )); } if (i==days) { map.put( "end" , endDate); } else { map.put( "end" , sdf.parse(getPreviouslyDate(startDate, "day" , i+ 1 )+ " 00:00:00" )); } list.add(map); } return list; } /** * @Title: getIntersectionDate 先进行排序,然后去中间的两个Date 判断此两端分钟数是否相等 不等则取到这个的交集 * @Description: 判断两个时间区间是否有交集 有 则返回交集部分 无则null 网上的一坨屎,自己写吧 * @param: @param bt 开始1 * @param: @param ot 结束1 * @param: @param st 开始2 * @param: @param ed 结束2 * @param: @return * @throws 包福平 * @return: List<Date> */ public static List<Date> getIntersectionDate(Date bt,Date ot,Date st,Date ed) { try { //去除直接没有任何交集的部分 if (bt.after(ed)||ot.before(st)) { return null ; } List<Date> returnList = new ArrayList<Date>(); List<Date> list = new ArrayList<Date>(); list.add(bt); list.add(ot); list.add(st); list.add(ed); Collections.sort(list); if (list.get( 1 ).compareTo(list.get( 2 ))!= 0 &&(bt.before(ed))) { SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); System.out.println( "包含的开始时间是:" + sdf.format(list.get( 1 )) + "-结束时间是:" + sdf.format(list.get( 2 ))); returnList.add(list.get( 1 )); returnList.add(list.get( 2 )); } return returnList; } catch (Exception e) { e.printStackTrace(); } return null ; } public static void main(String[] args) throws ParseException{ // String startTime="2019-01-12 7:30:33"; // String endTime="2019-01-14 7:30:34"; SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); // Date startDate=sdf.parse(startTime); // Date endDate=sdf.parse(endTime); // List<Map<String,Date>> list= getDayDetailsBetweenDates(startDate, endDate); // // for (Map<String, Date> map : list) { // System.out.println(sdf.format(map.get("start"))+"----"+sdf.format(map.get("end"))); // } // // 标准时间 Date startTime = sdf.parse( "2019-01-13 00:00:00" ); //startTime Date endTime = sdf.parse( "2019-01-14 00:00:00" ); //endTime // 目标时间 Date start = sdf.parse( "2019-01-13 07:30:00" ); //start Date end = sdf.parse( "2019-01-13 10:00:00" ); //end getIntersectionDate(startTime, endTime, start, end); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?