java 获取指定天数之前的日期

目的:获取当前时间前n天的日期  (示例就以30天为例)

运行代码:

返回的类型 java.sql.Date

        long time = 30*86400000;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date(System.currentTimeMillis() - time));
        System.out.println("获取到三十天前的日期为:"+java.sql.Date.valueOf(format));

 

粗看代码是没有问题,运行出来的时间竟然是:

 

今天是5月30日,三十天前是6月19日。。。

 debug断点。。。

 

int的取值范围为: -2^31——2^31-1,即-2147483648——2147483647 

。。。

强转啊。。数据类型

30天之前的时间是259.。。。而int的取值范围是214.。。。。强转成负数。。而用long类型的值就不会。。

 

记一个坑。。

附录:

java计算当前时间指定天数之前的日期,指定时间日期指定天数之前的日期。。的工具类

 

        //方法一
        long time = (long)30*86400000;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = simpleDateFormat.format(new Date(System.currentTimeMillis() - time));
        System.out.println("方法一:获取到三十天前的日期为:"+java.sql.Date.valueOf(format1));

        //方法二
        SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM-dd");
        String maxDateStr = "2018-01-01";
        String minDateStr = "";
        Calendar calc =Calendar.getInstance();
        try {
            calc.setTime(sdf.parse(maxDateStr));
            calc.add(calc.DATE, -30);
            Date minDate = calc.getTime();
            minDateStr = sdf.format(minDate);
            System.out.println("方法二获取指定日期2018-01-01之前的30天的日期为:"+minDateStr);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        //方法三
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -30);
        String endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now.getTime());
        System.out.println("方法三:获取当前时间前30天的日期,时分秒:"+endDate);

        //方法四
        LocalDateTime now1 = LocalDateTime.now();
        now1 = now1.minus(30, ChronoUnit.DAYS);
        System.out.println("方法四:java8新特性获取当前时间30天的日期"+now1.toString());

        //方法五
        Date now2 = new Date();
        Date startDate = DateUtils.addDays(now2, -30);
        System.out.println("方法五:lang3中的工具类DateUtils获取当前日期30天的日期为:"+startDate);
        //方法六
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();

        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        String day = format.format(d);
        System.out.println("方法六,当前时间前七天:"+day);

        //过去一月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -1);
        Date m = c.getTime();
        String mon = format.format(m);
        System.out.println("方法六,当前时间过去一个月:"+mon);

        //过去三个月
        c.setTime(new Date());
        c.add(Calendar.MONTH, -3);
        Date m3 = c.getTime();
        String mon3 = format.format(m3);
        System.out.println("方法六,当前时间过去三个月:"+mon3);

        //过去一年
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = format.format(y);
        System.out.println("方法六,当前时间,过去一年:"+year);

 

每个都进行验证了。。。妈妈再也不用担心我的时间计算了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值