123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- 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<String, Date> getToday() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getYesterday() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getThisWeek() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getLastWeek() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getThisMonth() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getLastMonth() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getThisYear() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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<String, Date> getLastYear() {
- Hashtable<String, Date> temp = new Hashtable<String, Date>();
- 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();
- }
- }
|