Java-日期判断及转换格式代码-将近7天数据补齐,添0

group by 分组出来统计的时候,不能补全,所以用java代码把没有时间的数据补全

public class Test {

    @org.junit.Test
    public void m1() {

        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 6, 0, 0, 0);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List<String> strings = new ArrayList<>();
        strings.add("2019-08-12");
        strings.add("2019-08-14");

        //存在list的哪个位置
        int flag = 0;
        //为0 近七天
        for (int i = 0; i < 6; i++) {
            if (!strings.contains(sdf.format(calendar.getTime()))){
                strings.add(flag,sdf.format(calendar.getTime()));

            }
            flag ++;
            calendar.add(Calendar.DAY_OF_MONTH, 1);
//            System.out.println(sdf.format(calendar.getTime()));
        }
//
        for (int i = 0; i < strings.size(); i++) {
            System.out.println(strings.get(i));
        }

    }
   

	/**
	获取近7天的零点
	*/
    public static Long getWeekZero() {
        Calendar calendar = Calendar.getInstance();

        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH) - 6, 0, 0, 0);
        Long zero = calendar.getTime().getTime();
        return zero;
    }
}

补全近72小时的时间

 @org.junit.Test
    public void m2() {


        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH");

        List<String> strings = new ArrayList<>();
        strings.add("2019-08-13 22");
        strings.add("2019-08-13 23");
        Calendar calendar1 = Calendar.getInstance();

        calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH) , calendar1.get(Calendar.HOUR_OF_DAY)-72, 0, 0);

        int flag = 0;//设置在哪里

        //为1 小时
        for (int i = 0; i < 72; i++) {

            if (!strings.contains(sdf2.format(calendar1.getTime()))){
                strings.add(flag,sdf2.format(calendar1.getTime()));

            }
            flag ++;
            calendar1.add(Calendar.HOUR_OF_DAY, 1);
//            System.out.println(sdf2.format(calendar1.getTime()));
        }
        for (int i = 0; i < strings.size(); i++) {
            System.out.println(strings.get(i));
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值