Java 日期工具类
public class CalendarUtil { private static Calendar calendar = null; /** * 根据类型抓取时间段 * @param type <dt> <dl>1:最近一个小时</dl> <dl>2:最近一天</dl> <dl>3:最近一周</dl> * <dl>4:最近一月</dl> <dl>5:最近一季度</dl> <dl>6:最近半年</dl> <dl>7:最近一年</dl> * <dl>8:昨天</dl><dl>9:上周</dl> <dl>10:上月</dl> <dl>11:上季度</dl> <dl>12:上半年</dl> * <dl>13:去年</dl> <dt> * @return */ public static Date[] getTime(int type) { switch (type) { case 1: return calcThePastHour(); case 2: return calcRecentDay(); case 3: return calcTheLatestWeek(); case 4: return calcRecentMonth(); case 5: return calcMostRecentQuarter(); case 6: return calcRecentHalfYear(); case 7: return null; case 8: return calcYesterday(); case 9: return calcLastWeek(); case 10: return calcLastMonth(); case 11: return calcLastSeason(); case 12: return calcFirstHalfYear(); case 13: return calcPastYear(); default: throw new IllegalArgumentException("type 只能在[1,13]之间"); } } /** * 昨天时间段 */ public static Date[] calcYesterday() { calendar = Calendar.getInstance(); calendar.add(Calendar.DATE, -1); Date from = getMinDate(calendar); Date to = getMaxDate(calendar); return new Date[]{from, to}; } /** * 国内习惯 上一周时间段 */ public static Date[] calcLastWeek() { calendar = Calendar.getInstance(); int minus = calendar.get(Calendar.DAY_OF_WEEK); calendar.add(Calendar.DATE, -minus + 1); Date to = getMaxDate(calendar); calendar.add(Calendar.DATE, -6); Date from = getMinDate(calendar); return new Date[]{from, to}; } /** * 国外习惯 上一周时间段 */ public static Date[] calcLastWeekF() { calendar = Calendar.getInstance(); int minus = calendar.get(Calendar.DAY_OF_WEEK); calendar.add(Calendar.DATE, -minus); Date to = getMaxDate(calendar); calendar.add(Calendar.DATE, -6); Date from = getMinDate(calendar); return new Date[]{from, to}; } /** * 上个月时间段 */ public static Date[] calcLastMonth() { calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, 0); Date to = getMaxDate(calendar); calendar.set(Calendar.DAY_OF_MONTH, 1); Date from = getMinDate(calendar); return new Date[]{from, to}; } /** * 上半年时间段 */ public static Date[] calcFirstHalfYear() { calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); calendar.set(year, 0, 1); Date from = getMinDate(calendar); calendar.set(year, 5, 30); Date to = getMaxDate(calendar); return new Date[]{from, to}; } /** * 下半年时间段 */ public static Date[] calcNextHalfYear() { calendar = Calendar.getInstance(); int year = calendar.get(Calendar.YEAR); calendar.set(year, 6, 1); Date from = getMinDate(calendar); calendar.set(year, 11, 31); Date to = getMaxDate(calendar); return new Date[]{from, to}; } /** * 去年时间段 */ public static Date[] calcPastYear() { calendar = Calendar.getInstance(); int pastYear = calendar.get(Calendar.YEAR) - 1; calendar.set(pastYear, 0, 1); Date from = getMinDate(calendar); calendar.set(pastYear, 11, 31); Date to = getMaxDate(calendar); return new Date[]{from, to}; } /** * 上一季度时间段 */ public static Date[] calcLastSeason() { calendar = Calendar.getInstance(); Date[] time = new Date[2]; Calendar c = Calendar.getInstance(); int lastSeason = getSeason(calendar) - 1; if (lastSeason == 1) {//第一季度 c.set(Calendar.MONTH, Calendar.JANUARY); time[0] = getMinMonthDate(c); c.set(Calendar.MONTH, Calendar.MARCH); time[1] = getMaxMonthDate(c); } else if (lastSeason == 2) {//第二季度 c.set(Calendar.MONTH, Calendar.APRIL); time[0] = getMinMonthDate(c); c.set(Calendar.MONTH, Calendar.JUNE); time[1] = getMaxMonthDate(c); } else if (lastSeason == 3) {//第三季度 c.set(Calendar.MONTH, Calendar.JULY); time[0] = getMinMonthDate(c); c.set(Calendar.MONTH, Calendar.SEPTEMBER); time[1] = getMaxMonthDate(c); } else if (lastSeason == 0) {//去年的第四季度 c.set(Calendar.MONTH, Calendar.OCTOBER); time[0] = getMinMonthDate(c); c.set(Calendar.MONTH, Calendar.DECEMBER); time[1] = getMaxMonthDate(c); } return time; } /** * 最近一小时时间段 */ public static Date[] calcThePastHour() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - 1); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 最近一天时间段 */ public static Date[] calcRecentDay() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 1); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 最近一周时间段 */ public static Date[] calcTheLatestWeek() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.WEEK_OF_MONTH, calendar.get(Calendar.WEEK_OF_MONTH) - 1); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 最近一个月 */ public static Date[] calcRecentMonth() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 最近一个季度 */ public static Date[] calcMostRecentQuarter() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 3); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 最近半年 */ public static Date[] calcRecentHalfYear() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 6); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 最近一年 */ public static Date[] calcRecentYear() { calendar = Calendar.getInstance(); Date to = calendar.getTime(); calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1); Date from = calendar.getTime(); return new Date[]{from, to}; } /** * 抓取季度序列号 * @param calendar * @return */ public static int getSeason(Calendar calendar) { int season = 0; int month = calendar.get(Calendar.MONTH); switch (month) { case Calendar.JANUARY: case Calendar.FEBRUARY: case Calendar.MARCH: season = 1; break; case Calendar.APRIL: case Calendar.MAY: case Calendar.JUNE: season = 2; break; case Calendar.JULY: case Calendar.AUGUST: case Calendar.SEPTEMBER: season = 3; break; case Calendar.OCTOBER: case Calendar.NOVEMBER: case Calendar.DECEMBER: season = 4; break; default: break; } return season; } /** * 获取当天最小时间 */ public static Date getMinDate(Calendar calendar) { calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 获取当天最大时间 */ public static Date getMaxDate(Calendar calendar) { calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 0); return calendar.getTime(); } /** * 获取一个月中第一天 */ public static Date getMinMonthDate(Calendar c) { c.set(Calendar.DAY_OF_MONTH, 1); return getMinDate(c); } /** * 获取一个月的最后一天 */ public static Date getMaxMonthDate(Calendar c) { c.add(Calendar.MONTH, 1); c.set(Calendar.DAY_OF_MONTH, 0); return getMaxDate(c); } }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!