package com.petrel;import java.util.Calendar;import java.util.Date;import java.util.HashMap;import java.util.Map;public class CalendarUtil{ private static Calendar calendar = null; /** * 根据类型抓取时间段 * @param type
1:最近一个小时
2:最近一天
3:最近一周
*
4:最近一月
5:最近一季度
6:最近半年
7:最近一年
*
8:昨天
9:上周
10:上月
11:上季度
12:上半年
*
13:去年
* @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 calcRecentYear(); 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); } /** * 计算2天之间的时间差 * @param early 大的 * @param late 小的 * @return */ public static final int daysBetween(Date early, Date late,Calendar c) { c.setTime(early); //设置时间为0时 c.set(java.util.Calendar.HOUR_OF_DAY, 0); c.set(java.util.Calendar.MINUTE, 0); c.set(java.util.Calendar.SECOND, 0); Date ed = c.getTime(); c.setTime(late); c.set(java.util.Calendar.HOUR_OF_DAY, 0); c.set(java.util.Calendar.MINUTE, 0); c.set(java.util.Calendar.SECOND, 0); Date ld = c.getTime(); //得到两个日期相差的天数 int days = ((int) (ed.getTime() / 1000) - (int) (ld.getTime() / 1000)) / 3600 / 24; return days; } /** * 查询指定时间段 [from,to]散落到指定年各个季度的天数情况 */ public static final Map breakDownDate(Date from, Date to,String year){ Map map = new HashMap (); // Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, Integer.valueOf(year)); // //1季度开始时间 c.set(Calendar.MONTH, Calendar.JANUARY);//1.1 Date t1Date = CalendarUtil.getMinMonthDate(c); // //2.二季度开始时间 c.set(Calendar.MONTH, Calendar.APRIL);//4.1 Date t2Date = CalendarUtil.getMinMonthDate(c); //3.三季度开始时间 c.set(Calendar.MONTH, Calendar.JULY);//7.1 Date t3Date = CalendarUtil.getMinMonthDate(c); //4.四季度开始时间 c.set(Calendar.MONTH, Calendar.OCTOBER); Date t4Date = CalendarUtil.getMinMonthDate(c);//10.1 c.add(Calendar.YEAR, 1);// c.set(Calendar.MONTH, Calendar.JANUARY);//来年一月一号 Date t5Date = CalendarUtil.getMinMonthDate(c); //Q1 map.put("Q1", dateintersection(from, to, t1Date, t2Date, c)); //Q2 map.put("Q2", dateintersection(from, to, t2Date, t3Date, c)); //Q3 map.put("Q3", dateintersection(from, to, t3Date, t4Date, c)); //Q4 map.put("Q4", dateintersection(from, to, t4Date, t5Date, c)); return map; } /** * 计算时间区间的交集 * @param a1 * @param a2 * @param b1 * @param b2 * @param c * @return 0 没有交集 */ public static final int dateintersection(Date a1,Date a2,Date b1,Date b2,Calendar c){ if(a2 == null){ a2 = b2; } Date min2,max1,max2; int cp = a1.compareTo(b1); if(cp > 0){ //min1 = b1; min2 = b2; max1 = a1; max2 = a2; }else{ //min1 = a1; min2 = a2; max1 = b1; max2 = b2; } if(max1.before(min2)){ if(min2.before(max2)){ return daysBetween(min2, max1, c); }else{ return daysBetween(max2, max1, c); } } return 0; }}