package zxc.utils; import java.util.Calendar; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import zxc.exceptions.LogicalException; public class CalendarUtil { protected static Log log = LogFactory.getLog(CalendarUtil.class); /** 设置一个星期的第一天 */ public static final int FIRST_DAY_OF_WEEK = Calendar.SUNDAY; /** 设置一年或一个月的第一个星期的最小天数 */ public static final int MINIMAL_DAYS_IN_FIRST_WEEK = 1; /** * 取得当前日期是多少周,星期日被定为一周的第一天,第一周允许的最小天数为1 * * @param date * @return */ public static int getWeekOfYear(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.setTime(date); return c.get(Calendar.WEEK_OF_YEAR); } public static int getWeekOfyear(int year, int month, int date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, month, date); return getWeekOfYear(c.getTime()); } /** * 总体上可以说是求一年的总周数,因为52*7=364,所以基本上此值应该为52,但是这里所要求的是一年中的最 * 后几天的WEEK_OF_YEAR的值,为什么不直接取12月31日的WEEK_OF_YEAR的值呢,这是因为根据 * setFirstDayOfWeek与setMinimalDaysInFirstWeek设置的值12月31日所WEEK_OF_YEAR字段所返回的值 * 很可能是下一年的第一个星期 * * @param year * @return */ public static int getMaxWeekNumOfYear(int year) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, Calendar.DECEMBER, 31); return c.get(Calendar.WEEK_OF_YEAR) == 1 ? 52 : c.get(Calendar.WEEK_OF_YEAR); } /** * 获取某年第一周的天数 * * @param year * @return */ public static int getDaysOfFirstWeek(int year) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, Calendar.JANUARY, 1); return 8 - c.get(Calendar.DAY_OF_WEEK); } /** * 取得当前日期所在周的第一天 * * @param date * @return */ public static Date getFirstDayOfWeek(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); return c.getTime(); } public static Date getFirstDayOfWeek(int year, int month, int date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, month, date); return getFirstDayOfWeek(c.getTime()); } /** * 取得当前日期所在周的最后一天 * * @param date * @return */ public static Date getLastDayOfWeek(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() == 1 ? 7 : (c.getFirstDayOfWeek() + 6) % 7); return c.getTime(); } public static Date getLastDayOfWeek(int year, int month, int date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, month, date); return getLastDayOfWeek(c.getTime()); } /** * 获取两个日期间的周次差,这两个日期要么在同一年,要么是相邻的两个年份 * * @param start * @param end * @return */ public static int getDeltaWeeks(Date start, Date end) { Calendar cs = Calendar.getInstance(); Calendar ce = Calendar.getInstance(); cs.setTime(start); ce.setTime(end); int s = getWeekOfYear(start); int e = getWeekOfYear(end); return cs.get(Calendar.YEAR) == ce.get(Calendar.YEAR) ? e - s : e - s + getMaxWeekNumOfYear(cs.get(Calendar.YEAR)); } public static int getDeltaWeeks2(Date start, Date end) { Calendar cs = Calendar.getInstance(); Calendar ce = (Calendar) cs.clone(); Calendar ct = Calendar.getInstance(); ct.setTime(start); cs.set(ct.get(Calendar.YEAR), ct.get(Calendar.MONTH), ct.get(Calendar.DATE)); ct.setTime(end); ce.set(ct.get(Calendar.YEAR), ct.get(Calendar.MONTH), ct.get(Calendar.DATE)); cs.setTime(getFirstDayOfWeek(cs.getTime())); ce.setTime(getFirstDayOfWeek(ce.getTime())); return (int) ((ce.getTimeInMillis() - cs.getTimeInMillis()) / (7 * 24 * 60 * 60 * 1000) + 1); } /** * 得到某年某周的第一天 * * @param year * @param week * @return */ public static Date getFirstDayOfWeek(int year, int week) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, Calendar.JANUARY, 1); c.add(Calendar.DATE, week * 7); return getFirstDayOfWeek(c.getTime()); } /** * 得到某年某周的最后一天 * * @param year * @param week * @return */ public static Date getLastDayOfWeek(int year, int week) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.set(year, Calendar.JANUARY, 1); c.add(Calendar.DATE, week * 7); return getLastDayOfWeek(c.getTime()); } /** * 获取当前日期所在月份的第一天 * * @param date * @return */ public static Date getFirstDayOfMonth(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, 1); return c.getTime(); } /** * 获取当前日期所在月份的最后一天 * * @param date * @return */ public static Date getLastDayOfMonth(Date date) { Calendar c = Calendar.getInstance(); c.setFirstDayOfWeek(FIRST_DAY_OF_WEEK); c.setMinimalDaysInFirstWeek(MINIMAL_DAYS_IN_FIRST_WEEK); c.setTime(date); c.set(Calendar.DAY_OF_MONTH, 1);//日期置1 c.add(Calendar.MONTH, 1);//月份向前添加一个月 c.add(Calendar.DAY_OF_MONTH, -1);//日期-1 return c.getTime(); } /** * 获取当前日期所在月份的最后一天 * * @param date * @return */ public static long getDaysOfTwoDate(Date start, Date end) { Calendar c = Calendar.getInstance(); Calendar temp = Calendar.getInstance(); //取年月日 c.setTime(start); temp.clear(); temp.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); start = temp.getTime(); System.out.println(start); //取年月日 c.setTime(end); temp.clear(); temp.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)); end = temp.getTime(); System.out.println(end); long time = (end.getTime() - start.getTime()) / (24 * 60 * 60 * 1000); return time + 1; } public static int getYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int year = c.get(Calendar.YEAR); return year; } public static int getMothOfYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int month = c.get(Calendar.MONTH); return month+1; } public static int getHalfOfYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int month = c.get(Calendar.MONTH); return month <= 5 ? 1 : 2; } public static int getQuarterOfYear(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int month = c.get(Calendar.MONTH); switch (month) { case 0: case 1: case 2: return 1; case 3: case 4: case 5: return 2; case 6: case 7: case 8: return 3; case 9: case 10: case 11: return 4; default: throw new LogicalException("不可能存在其它的月份值"); } } public static int getQuarterOfYear2(Date date) { Calendar c = Calendar.getInstance(); c.setTime(date); int month = c.get(Calendar.MONTH); return month / 3 + 1; } public static void main(String[] args) { Date start = new Date(); Date end1 = new Date(start.getTime() + 60 * 60 * 1000); Date end2 = new Date(start.getTime() + 7 * 60 * 60 * 1000); Date end3 = new Date(start.getTime() + 8 * 60 * 60 * 1000); Date end4 = new Date(start.getTime() + 20 * 60 * 60 * 1000); Date end5 = new Date(start.getTime() + 24 * 60 * 60 * 1000); System.out.println(getDaysOfTwoDate(start, end1)); System.out.println(getDaysOfTwoDate(start, end2)); System.out.println(getDaysOfTwoDate(start, end3)); System.out.println(getDaysOfTwoDate(start, end4)); System.out.println(getDaysOfTwoDate(start, end5)); } }