java获取不同时间的工具类

来源:互联网 发布:sql注入防范 编辑:程序博客网 时间:2024/06/05 02:35
 // @author  // 计算本周  上周  前三月  今年  去年 的时间段 // 组装时间  用于计算同比和环比的private  Map<String, Object>  getcurrlastweek(Map<String, Object> map){    String isdata = (String) map.get("range");    boolean bool = false;    if(StringUtil.isBlank(isdata)|| "all".equals(isdata)){        return map;    }    Calendar cal = Calendar.getInstance(Locale.CHINA);    Calendar cal2 = Calendar.getInstance(Locale.CHINA);    //n为推迟的周数,1本周,-1向前推迟一周,2下周,依次类推    int n = 1;    String monday = null;    String sunday = null;    String yearbegin = null;    String yearend = null;    switch(isdata){         case "thisWeek":                bool = true;                cal.setFirstDayOfWeek(Calendar.MONDAY);                cal.add(Calendar.DATE, -1*7);                //当前时间,貌似多余,其实是为了所有可能的系统一致                cal.setTimeInMillis(System.currentTimeMillis());               //想周几,这里就传几Calendar.MONDAY(TUESDAY...)                cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);                monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal2.setTime(cal.getTime());                cal2.add(Calendar.YEAR, -1);                yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());                cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);                sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal.add(Calendar.YEAR, -1);                yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());         break;         case "lastWeek" :                bool = true;                cal.setFirstDayOfWeek(Calendar.MONDAY);                cal.add(Calendar.DATE, -2*7);                 //当前时间,貌似多余,其实是为了所有可能的系统一致                cal.setTimeInMillis(System.currentTimeMillis());                cal.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);                monday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal2.setTime(cal.getTime());                cal2.add(Calendar.YEAR, -1);                yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());                cal.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);                sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal.add(Calendar.YEAR, -1);                yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                break ;         case "lastMonth":                cal.setTimeInMillis(System.currentTimeMillis());                int month = cal.get(Calendar.MONTH);                cal.set(Calendar.MONTH, month-2);                cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));                  sunday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal2.setTime(cal.getTime());                cal2.add(Calendar.YEAR, -1);                yearend= new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());                cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));                  monday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal.add(Calendar.YEAR, -1);                yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                break;         case "lastThreeMonth":                cal.setTimeInMillis(System.currentTimeMillis());                int month2 = cal.get(Calendar.MONTH);                cal.set(Calendar.MONTH, month2-6);                cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH));                  monday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal2.setTime(cal.getTime());                cal2.add(Calendar.YEAR, -1);                yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());                int month3 = cal.get(Calendar.MONTH);                cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH));                 cal.set(Calendar.MONTH, month3+3);                sunday= new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                cal.add(Calendar.YEAR, -1);                yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                break;         case "thisYear":                 int currentYear = cal.get(Calendar.YEAR)-1;                 cal.clear();                 cal.set(Calendar.YEAR, currentYear);                 Date currYearFirst = cal.getTime();                 monday= new SimpleDateFormat("yyyy-MM-dd").format(currYearFirst);                 cal2.setTime(cal.getTime());                 cal2.add(Calendar.YEAR, -1);                // yearbegin = new SimpleDateFormat("yyyy-MM-dd").format(cal2.getTime());                 yearbegin = monday;                 cal.clear();                 cal.set(Calendar.YEAR, currentYear);                 cal.roll(Calendar.DAY_OF_YEAR, -1);                 Date currYearLast = cal.getTime();                 sunday = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                 cal.add(Calendar.YEAR, -1);               //  yearend = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());                 yearend = sunday;                break;         default:                break;                   }
0 0
原创粉丝点击