java获取不同时间的工具类

 // @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;               
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值