简体   繁体   English

每10分钟修改一次Java日历功能

[英]Modify Java calendar function every 10 minutes

I am looking to modify this java calendar function, I am getting what I need to be output every 10 minutes, but its only showing at minute 0 of every hour. 我正在寻找修改此Java日历功能的方法,我需要每10分钟输出一次,但仅在每小时的0分钟显示。 I would need that this would be showing for the past 10 minutes. 我需要在过去10分钟内显示出来。 So an example of current behaviour: 因此,一个当前行为的例子:

if current time is 3:26 PM: 如果当前时间是3:26 PM:

currently function is showing this result: 2:50 PM - 3:00 PM (it stops at minute 0 of every hour) 当前功能正在显示以下结果:2:50 PM-3:00 PM(它在每小时的0分钟停止)

correct behaviour would be showing last 10 minutes of last period: 3:10 PM - 3:20 PM (when its 3:32 PM it should show: 3:20 PM - 3:30 PM) 正确的行为将显示上一周期的最后10分钟:3:10 PM-3:20 PM(当其3:32 PM时应显示:3:20 PM-3:30 PM)

How can modify the function to make it work as I need? 如何修改功能以使其按需工作?

SimpleDateFormat fth = new SimpleDateFormat("hh:mm a");
SimpleDateFormat ftd = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Calendar ca = Calendar.getInstance();
ca.setTime(new Date());

ca.set(Calendar.MINUTE, 0);


for(int i=0;i<24;i++){
    Date timeTo = ca.getTime();

    ca.add(Calendar.MINUTE, -10);

    Date timeFrom = ca.getTime();

Hope that you understand what I mean. 希望你明白我的意思。 Thank you. 谢谢。

You are setting the minute to 0 in your code. 您正在代码中将分钟设置为0。

ca.set(Calendar.MINUTE, 0);

Instead you may want. 相反,您可能想要。

ca.set(Calendar.MINUTE, (ca.get(Calendar.MINUTE) / 10) * 10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM