How to increase time at 10 minute intervals

For a specific date (this is a hard-coded date), I want to increase the time by LastModifiedTimeFromand LastModifiedTimeToby 10 minutes each time.

I have tried the following but are not incrementing and I am not sure why?

Date d = Date.parse("HH:mm:ss", "00:00:00")
Calendar c = Calendar.getInstance()
c.setTime(d)

c.add(MINUTE, 10)
assert c.time == "00:10:00"

c.add(MINUTE, 10)
assert c.time == "00:20:00"

c.add(MINUTE, 10)
assert c.time == "00:30:00"
+6
source share
1 answer

I would recommend using a combined approach using Groovy syntactic sugar and a verbose but reliable Java calendar. Using the code below, you can analyze the date and start adding 10 minutes to it, checking it when you go ahead.

import java.util.Calendar
import static java.util.Calendar.*

Date d = Date.parse("HH:mm:ss", "00:00:00")
Calendar c = Calendar.getInstance()
c.setTime(d)

c.add(MINUTE, 10)
assert c.time.format("HH:mm:ss") == "00:10:00"

c.add(MINUTE, 10)
assert c.time.format("HH:mm:ss") == "00:20:00"

c.add(MINUTE, 10)
assert c.time.format("HH:mm:ss") == "00:30:00"
+3
source

Source: https://habr.com/ru/post/1014672/


All Articles