package com.sinosoft.lz.common; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Hashtable; /** * 功能:今天,昨天,本周,上周,本月,上月 范围:以周一作为一周的第一天,每周天数7天 * * @author wzj * */ public class CalendarUtil{ private Date begin; private Date end; private Date now; // private Calendar now ; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public CalendarUtil() { now = new Date(); // now = Calendar.getInstance(); } // 获取今天 public Hashtable getToday() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); end = now; temp.put("begin", begin); temp.put("end", end); return temp; } // 获取昨天 @SuppressWarnings("static-access") public Hashtable getYesterday() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); cal.add(cal.DATE, -1); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.HOUR_OF_DAY, 23); end = cal.getTime(); temp.put("end", end); return temp; } // 获取本周 public Hashtable getThisWeek() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); end = cal.getTime(); int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2; cal.add(Calendar.DATE, -day_of_week); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); // cal.add(Calendar.DATE, 6); // cal.set(Calendar.MILLISECOND, 0); // cal.set(Calendar.SECOND, 59); // cal.set(Calendar.MINUTE, 59); // cal.set(Calendar.HOUR_OF_DAY, 23); temp.put("end", end); return temp; } // 获取上周 @SuppressWarnings("static-access") public Hashtable getLastWeek() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); cal.add(cal.WEEK_OF_YEAR, -1); int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2; cal.add(Calendar.DATE, -day_of_week); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); cal.add(Calendar.DATE, 6); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.HOUR_OF_DAY, 23); end = cal.getTime(); temp.put("end", end); return temp; } // 获取本月 @SuppressWarnings("static-access") public Hashtable getThisMonth() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); end = cal.getTime(); // cal.add(field, amount)\ cal.set(cal.DAY_OF_MONTH, 1); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); temp.put("end", end); return temp; } // 获取上月 @SuppressWarnings("static-access") public Hashtable getLastMonth() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); cal.set(cal.DAY_OF_MONTH, 0); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.HOUR_OF_DAY, 23); // cal.add(cal., amount) end = cal.getTime(); cal.set(cal.DAY_OF_MONTH, 1); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); temp.put("end", end); return temp; } // 获取本年 @SuppressWarnings("static-access") public Hashtable getThisYear() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); end = cal.getTime(); cal.set(cal.DAY_OF_YEAR, 1); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); temp.put("end", end); return temp; } // 获取上一年 @SuppressWarnings("static-access") public Hashtable getLastYear() { Hashtable temp = new Hashtable(); Calendar cal = Calendar.getInstance(); cal.set(cal.DAY_OF_YEAR, 0); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 59); cal.set(Calendar.MINUTE, 59); cal.set(Calendar.HOUR_OF_DAY, 23); // cal.add(cal., amount) end = cal.getTime(); cal.set(cal.DAY_OF_YEAR, 1); cal.set(Calendar.MILLISECOND, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.HOUR_OF_DAY, 0); begin = cal.getTime(); temp.put("begin", begin); temp.put("end", end); return temp; } /** * @param args */ public static void main(String[] args) { // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // Calendar cal = Calendar.getInstance(); // cal.add(cal.WEEK_OF_YEAR, -1); // cal.set(cal.DAY_OF_MONTH, 1); // cal.set(cal.DAY_OF_YEAR, 0); // sdf.format(date) // cal.set(Calendar.HOUR_OF_DAY, 0); // cal.set(Calendar.MILLISECOND, 0); // cal.set(Calendar.SECOND, 0); // cal.set(Calendar.MINUTE, 0); // int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - 2; // cal.add(Calendar.DATE, -day_of_week); // // cal.add(Calendar.DATE, 6); CalendarUtil cu = new CalendarUtil(); cu.getToday(); cu.getYesterday(); cu.getThisWeek(); cu.getLastWeek(); cu.getThisMonth(); cu.getLastMonth(); cu.getThisYear(); cu.getLastYear(); } }