日、周、月、年时间段计算

这样的时间段一般用于大屏上的数据展示。

public String getKssj(String type){
        String kssj = null;
        Date d=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        if("day".equals(type)){
            kssj = df.format(d);
        }else if("week".equals(type)){
            c.add(Calendar.DATE, - 6);
            Date date = c.getTime();
            kssj = df.format(date);
        }else if ("month".equals(type)) {
            c.add(Calendar.MONTH, - 1);
            c.add(Calendar.DATE, + 1);
            Date date = c.getTime();
            kssj = df.format(date);
        }else if("year".equals(type)){
            c.add(Calendar.YEAR, - 1);
            c.add(Calendar.MONTH, + 1);
            c.set(Calendar.DAY_OF_MONTH,1);
            Date date = c.getTime();
            kssj = df.format(date);
        }
        return kssj;
    }

备注:代码中的计算数量。这样自定义的静态常量按系统定义来或者自己来定义都可以,只要计算正确的时间段即可!

public final static int DATE = 5;
public final static int MONTH = 2;
public final static int YEAR = 1;
public final static int DAY_OF_MONTH = 5;

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值