时间格式转换和加一天操作

    @Test
    public void testDayAdd() {
        // 小时时间段
        String start = "09:30";
        String end = "00:00";

        // 时间格式
        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd ");

        Date date = new Date();

        // 当前时间格式
        String currentTimeFormat = sdfOne.format(date);

        // 时间段范围格式转换
        String startTimeFormat = sdfTwo.format(date) + start;
        String endTimeFormat = sdfTwo.format(date) + end;

        // 解析成日期格式
        Date currentDate = null;
        Date startDate = null;
        Date endDate = null;
        try {
            currentDate = sdfOne.parse(currentTimeFormat);
            startDate = sdfOne.parse(startTimeFormat);
            endDate = sdfOne.parse(endTimeFormat);
            if (end != null && end.startsWith("00")) {
                // 时间段结束以00 开头,日期时间加一天
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(endDate);
                // 日期加一天
                calendar.add(Calendar.DATE, 1);
                endDate = calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(currentDate);
        System.out.println(startDate);
        System.out.println(endDate);
        /**
         * 输出:
         * Wed May 04 23:28:00 CST 2022
         * Wed May 04 09:30:00 CST 2022
         * Thu May 05 00:00:00 CST 2022
         */
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值