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

2023-12-04  本文已影响0人  唏嘘的码农

//通用方法

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;

    }

//https://blog.csdn.net/weixin_52277015/article/details/112302829

上一篇 下一篇

猜你喜欢

热点阅读