这是一个java工具类,用于大多数需要判断日期是不是当天、本周、本月、本季度、本年,以更好的进行下一步判断,
那么如何去写代码吗?请直接看下面:
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
| import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateStatusUtil { public static boolean isToday(Date time) { return isThisTime(time, "yyyy-MM-dd"); } public static boolean isThisWeek(Date time) { Calendar calendar = Calendar.getInstance(); int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR); calendar.setTime(time); int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR); if (paramWeek == currentWeek) { return true; } return false; } public static boolean isThisMonth(Date time) { return isThisTime(time, "yyyy-MM"); } public static boolean isThisYear(Date time) { return isThisTime(time, "yyyy"); } public static boolean isThisQuarter(Date time) { Date QuarterStart = getCurrentQuarterStartTime(); Date QuarterEnd = getCurrentQuarterEndTime(); return time.after(QuarterStart) && time.before(QuarterEnd); } private static boolean isThisTime(Date time, String pattern) { SimpleDateFormat sdf = new SimpleDateFormat(pattern); String param = sdf.format(time); String now = sdf.format(new Date()); if (param.equals(now)) { return true; } return false; }
public static Date getCurrentQuarterStartTime() { Calendar c = Calendar.getInstance(); int currentMonth = c.get(Calendar.MONTH) + 1; SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd"); Date now = null; try { if (currentMonth >= 1 && currentMonth <= 3) c.set(Calendar.MONTH, 0); else if (currentMonth >= 4 && currentMonth <= 6) c.set(Calendar.MONTH, 3); else if (currentMonth >= 7 && currentMonth <= 9) c.set(Calendar.MONTH, 4); else if (currentMonth >= 10 && currentMonth <= 12) c.set(Calendar.MONTH, 9); c.set(Calendar.DATE, 1); now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00"); } catch (Exception e) { e.printStackTrace(); } return now; }
public static Date getCurrentQuarterEndTime() { Calendar cal = Calendar.getInstance(); cal.setTime(getCurrentQuarterStartTime()); cal.add(Calendar.MONTH, 3); return cal.getTime(); } public static void main(String[] args) { try { System.out.println("当天:" + DateStatusUtil .isToday(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-15 08:23:21"))); System.out.println("本周:" + DateStatusUtil .isThisWeek(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-25 08:23:21"))); System.out.println("本月:" + DateStatusUtil .isThisMonth(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-15 08:23:21"))); System.out.println("本年:" + DateStatusUtil .isThisYear(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2022-07-15 08:23:21"))); System.out.println("本季度:" + DateStatusUtil .isThisQuarter(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2021-07-15 08:23:21"))); } catch (Exception pe) { System.out.println(pe.getMessage()); }
} }
|
运行后输出:
1 2 3 4 5
| 当天:true 本周:false 本月:true 本年:false 本季度:true
|