Java获取本周、本月、本年、及各种时间段的工具类
1 import java.sql.Timestamp; 2 import java.util.ArrayList; 3 import java.util.Calendar; 4 import java.util.Date; 5 import java.util.GregorianCalendar; 6 import java.util.List; 7 8 public class DateUtils { 9 // 获取当天的开始时间 10 public static java.util.Date getDayBegin() { 11 Calendar cal = new GregorianCalendar(); 12 cal.set(Calendar.HOUR_OF_DAY, 0); 13 cal.set(Calendar.MINUTE, 0); 14 cal.set(Calendar.SECOND, 0); 15 cal.set(Calendar.MILLISECOND, 0); 16 return cal.getTime(); 17 } 18 19 // 获取当天的结束时间 20 public static java.util.Date getDayEnd() { 21 Calendar cal = new GregorianCalendar(); 22 cal.set(Calendar.HOUR_OF_DAY, 23); 23 cal.set(Calendar.MINUTE, 59); 24 cal.set(Calendar.SECOND, 59); 25 return cal.getTime(); 26 } 27 28 // 获取昨天的开始时间 29 public static Date getBeginDayOfYesterday() { 30 Calendar cal = new GregorianCalendar(); 31 cal.setTime(getDayBegin()); 32 cal.add(Calendar.DAY_OF_MONTH, -1); 33 return cal.getTime(); 34 } 35 36 // 获取昨天的结束时间 37 public static Date getEndDayOfYesterDay() { 38 Calendar cal = new GregorianCalendar(); 39 cal.setTime(getDayEnd()); 40 cal.add(Calendar.DAY_OF_MONTH, -1); 41 return cal.getTime(); 42 } 43 44 // 获取明天的开始时间 45 public static Date getBeginDayOfTomorrow() { 46 Calendar cal = new GregorianCalendar(); 47 cal.setTime(getDayBegin()); 48 cal.add(Calendar.DAY_OF_MONTH, 1); 49 50 return cal.getTime(); 51 } 52 53 // 获取明天的结束时间 54 public static Date getEndDayOfTomorrow() { 55 Calendar cal = new GregorianCalendar(); 56 cal.setTime(getDayEnd()); 57 cal.add(Calendar.DAY_OF_MONTH, 1); 58 return cal.getTime(); 59 } 60 61 // 获取本周的开始时间 62 public static Date getBeginDayOfWeek() { 63 Date date = new Date(); 64 if (date == null) { 65 return null; 66 } 67 Calendar cal = Calendar.getInstance(); 68 cal.setTime(date); 69 int dayofweek = cal.get(Calendar.DAY_OF_WEEK); 70 if (dayofweek == 1) { 71 dayofweek += 7; 72 } 73 cal.add(Calendar.DATE, 2 - dayofweek); 74 return getDayStartTime(cal.getTime()); 75 } 76 77 // 获取本周的结束时间 78 public static Date getEndDayOfWeek() { 79 Calendar cal = Calendar.getInstance(); 80 cal.setTime(getBeginDayOfWeek()); 81 cal.add(Calendar.DAY_OF_WEEK, 6); 82 Date weekEndSta = cal.getTime(); 83 return getDayEndTime(weekEndSta); 84 } 85 86 // 获取本月的开始时间 87 public static Date getBeginDayOfMonth() { 88 Calendar calendar = Calendar.getInstance(); 89 calendar.set(getNowYear(), getNowMonth() - 1, 1); 90 return getDayStartTime(calendar.getTime()); 91 } 92 93 // 获取本月的结束时间 94 public static Date getEndDayOfMonth() { 95 Calendar calendar = Calendar.getInstance(); 96 calendar.set(getNowYear(), getNowMonth() - 1, 1); 97 int day = calendar.getActualMaximum(5); 98 calendar.set(getNowYear(), getNowMonth() - 1, day); 99 return getDayEndTime(calendar.getTime()); 100 } 101 102 // 获取上个月的开始时间 103 public static Date getBeginDayOfLastMonth() { 104 Date date = new Date(); 105 Calendar calendar = Calendar.getInstance(); 106 calendar.setTime(date); 107 calendar.add(calendar.MONTH, -1); 108 calendar.set(calendar.DAY_OF_MONTH, 1); 109 return getDayStartTime(calendar.getTime()); 110 } 111 112 // 获取上个月的结束时间 113 public static Date getEndDayOfLastMonth() { 114 Date date = new Date(); 115 Calendar calendar = Calendar.getInstance(); 116 calendar.setTime(date); 117 calendar.add(calendar.MONTH, -1); 118 int day = calendar.getActualMaximum(5); 119 calendar.set(calendar.DAY_OF_MONTH, day); 120 return getDayEndTime(calendar.getTime()); 121 } 122 123 // 获取本年的开始时间 124 public static java.util.Date getBeginDayOfYear() { 125 Calendar cal = Calendar.getInstance(); 126 cal.set(Calendar.YEAR, getNowYear()); 127 // cal.set 128 cal.set(Calendar.MONTH, Calendar.JANUARY); 129 cal.set(Calendar.DATE, 1); 130 131 return getDayStartTime(cal.getTime()); 132 } 133 134 // 获取本年的结束时间 135 public static java.util.Date getEndDayOfYear() { 136 Calendar cal = Calendar.getInstance(); 137 cal.set(Calendar.YEAR, getNowYear()); 138 cal.set(Calendar.MONTH, Calendar.DECEMBER); 139 cal.set(Calendar.DATE, 31); 140 return getDayEndTime(cal.getTime()); 141 } 142 143 // 获取某个日期的开始时间 144 public static Timestamp getDayStartTime(Date d) { 145 Calendar calendar = Calendar.getInstance(); 146 if (null != d) 147 calendar.setTime(d); 148 calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 0, 149 0, 0); 150 calendar.set(Calendar.MILLISECOND, 0); 151 return new Timestamp(calendar.getTimeInMillis()); 152 } 153 154 // 获取某个日期的结束时间 155 public static Timestamp getDayEndTime(Date d) { 156 Calendar calendar = Calendar.getInstance(); 157 if (null != d) 158 calendar.setTime(d); 159 calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), 23, 160 59, 59); 161 calendar.set(Calendar.MILLISECOND, 999); 162 return new Timestamp(calendar.getTimeInMillis()); 163 } 164 165 // 获取今年是哪一年 166 public static Integer getNowYear() { 167 Date date = new Date(); 168 GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); 169 gc.setTime(date); 170 return Integer.valueOf(gc.get(1)); 171 } 172 173 // 获取本月是哪一月 174 public static int getNowMonth() { 175 Date date = new Date(); 176 GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance(); 177 gc.setTime(date); 178 return gc.get(2) + 1; 179 } 180 181 // 两个日期相减得到的天数 182 public static int getDiffDays(Date beginDate, Date endDate) { 183 184 if (beginDate == null || endDate == null) { 185 throw new IllegalArgumentException("getDiffDays param is null!"); 186 } 187 188 long diff = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24); 189 190 int days = new Long(diff).intValue(); 191 192 return days; 193 } 194 195 // 两个日期相减得到的毫秒数 196 public static long dateDiff(Date beginDate, Date endDate) { 197 long date1ms = beginDate.getTime(); 198 long date2ms = endDate.getTime(); 199 return date2ms - date1ms; 200 } 201 202 // 获取两个日期中的最大日期 203 public static Date max(Date beginDate, Date endDate) { 204 if (beginDate == null) { 205 return endDate; 206 } 207 if (endDate == null) { 208 return beginDate; 209 } 210 if (beginDate.after(endDate)) { 211 return beginDate; 212 } 213 return endDate; 214 } 215 216 // 获取两个日期中的最小日期 217 public static Date min(Date beginDate, Date endDate) { 218 if (beginDate == null) { 219 return endDate; 220 } 221 if (endDate == null) { 222 return beginDate; 223 } 224 if (beginDate.after(endDate)) { 225 return endDate; 226 } 227 return beginDate; 228 } 229 230 // 返回某月该季度的第一个月 231 public static Date getFirstSeasonDate(Date date) { 232 final int[] SEASON = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; 233 Calendar cal = Calendar.getInstance(); 234 cal.setTime(date); 235 int sean = SEASON[cal.get(Calendar.MONTH)]; 236 cal.set(Calendar.MONTH, sean * 3 - 3); 237 return cal.getTime(); 238 } 239 240 // 返回某个日期下几天的日期 241 public static Date getNextDay(Date date, int i) { 242 Calendar cal = new GregorianCalendar(); 243 cal.setTime(date); 244 cal.set(Calendar.DATE, cal.get(Calendar.DATE) + i); 245 return cal.getTime(); 246 } 247 248 // 返回某个日期前几天的日期 249 public static Date getFrontDay(Date date, int i) { 250 Calendar cal = new GregorianCalendar(); 251 cal.setTime(date); 252 cal.set(Calendar.DATE, cal.get(Calendar.DATE) - i); 253 return cal.getTime(); 254 } 255 256 // 获取某年某月到某年某月按天的切片日期集合(间隔天数的日期集合) 257 public static List getTimeList(int beginYear, int beginMonth, int endYear, int endMonth, int k) { 258 List list = new ArrayList(); 259 if (beginYear == endYear) { 260 for (int j = beginMonth; j <= endMonth; j++) { 261 list.add(getTimeList(beginYear, j, k)); 262 263 } 264 } else { 265 { 266 for (int j = beginMonth; j < 12; j++) { 267 list.add(getTimeList(beginYear, j, k)); 268 } 269 270 for (int i = beginYear + 1; i < endYear; i++) { 271 for (int j = 0; j < 12; j++) { 272 list.add(getTimeList(i, j, k)); 273 } 274 } 275 for (int j = 0; j <= endMonth; j++) { 276 list.add(getTimeList(endYear, j, k)); 277 } 278 } 279 } 280 return list; 281 } 282 283 // 获取某年某月按天切片日期集合(某个月间隔多少天的日期集合) 284 public static List getTimeList(int beginYear, int beginMonth, int k) { 285 List list = new ArrayList(); 286 Calendar begincal = new GregorianCalendar(beginYear, beginMonth, 1); 287 int max = begincal.getActualMaximum(Calendar.DATE); 288 for (int i = 1; i < max; i = i + k) { 289 list.add(begincal.getTime()); 290 begincal.add(Calendar.DATE, k); 291 } 292 begincal = new GregorianCalendar(beginYear, beginMonth, max); 293 list.add(begincal.getTime()); 294 return list; 295 } 296 297 // 获取某年某月的第一天日期 298 public static Date getStartMonthDate(int year, int month) { 299 Calendar calendar = Calendar.getInstance(); 300 calendar.set(year, month - 1, 1, 0, 0, 0); 301 return calendar.getTime(); 302 } 303 304 // 获取某年某月的最后一天日期 305 public static Date getEndMonthDate(int year, int month) { 306 Calendar calendar = Calendar.getInstance(); 307 calendar.set(year, month - 1, 1); 308 int day = calendar.getActualMaximum(5); 309 calendar.set(year, month - 1, day, 23, 59, 59); 310 return calendar.getTime(); 311 } 312 }
/** * 获取某年某月的第一天日期 */ private Date getStartMonthDate(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, 1,0,0,0); return calendar.getTime(); } /** * 获取某年某月的最后一天日期 */ private Date getEndMonthDate(int year, int month) { Calendar calendar = Calendar.getInstance(); calendar.set(year, month - 1, 1); int day = calendar.getActualMaximum(5); calendar.set(year, month - 1, day,23,59,59); return calendar.getTime(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结