求前五分钟所在的分钟数为5的倍数的时间段

项目中的需要用到的一个小的时间条件,表数据是每五分钟统计一次,查询的时间条件就是当前时间前五分钟所在的分钟数为5的倍数倍的时间段,着急将就写了一个,也没有什么好的思路

 

public static void main(String[] args) {
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try {
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - 5);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        SimpleDateFormat hourDf = new SimpleDateFormat("mm");

        String time=hourDf.format(calendar.getTime());
        //分钟数是5的多少倍
        int n=Integer.valueOf(time)/5;
        //五分钟前时间所在的小时
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
        calendar.setTime(format.parse(format.format(calendar.getTime())));
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)+5*n);
        String time2=df.format(calendar.getTime());
        calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE)+5);
        String time3=df.format(calendar.getTime());
        System.out.println(time2);
        System.out.println(time3);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

测试结果1:

输入时间:2020-06-05 15:01:00
前五分钟开始时间点:2020-06-05 14:55:00
前五分钟结束时间点:2020-06-05 15:00:00

 

测试结果2:

输入时间:2020-06-05 15:49:00
前五分钟开始时间点:2020-06-05 15:40:00
前五分钟结束时间点:2020-06-05 15:45:00

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值