user3903810
user3903810

Reputation: 19

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. I would need that this would be showing for the past 10 minutes. So an example of current behaviour:

if current time is 3:26 PM:

currently function is showing this result: 2:50 PM - 3:00 PM (it stops at minute 0 of every hour)

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)

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.

Upvotes: 0

Views: 139

Answers (1)

markbernard
markbernard

Reputation: 1420

You are setting the minute to 0 in your code.

ca.set(Calendar.MINUTE, 0);

Instead you may want.

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

Upvotes: 1

Related Questions