Java处理时间,得到指定几天前的凌晨0点时间戳

该代码示例展示了如何使用`SimpleDateFormat`和`Date`进行日期格式化,并计算指定天数前后的日期时间戳。主要功能包括将当前日期转换为指定格式的字符串,然后解析回日期对象,最后计算给定天数的时间戳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//通用方法
public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        /*ate parse = null;
        try {
            parse = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(sdf.format(date));
        System.out.println(parse.getTime());*/
        for (int i = 0; i < 30; i++) {
            System.out.println(sdf.format(timesTamp(i)));
            System.out.println(timesTamp(i));
        }
    }

    //返回指定天数时间戳(零点时刻)
    private static long timesTamp(int count) {
        Date date = new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(sdf.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (count == 0){
            return parse.getTime() + 24L * 60 * 60 * 1000;
        }
        count --;
        return parse.getTime() - count * 24L * 60 * 60 * 1000;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值