Is there any inbuilt class in java se / java ee representig an arbitrary "Time Resolution"; e.g every hour, every second, all 10 minutes etc. ?
-
2What do you want to accomplish? Schedule a task for periodic execution? – João Silva Sep 15 '12 at 15:15
-
@JoaoSilva No, actually I have to pass such a time resolution into a service so it can perform business logic on a set of data – Sebastian Hoffmann Sep 15 '12 at 15:19
-
This post may be of interest to you: http://stackoverflow.com/questions/351565/system-currenttimemillis-vs-system-nanotime – BlackVegetable Sep 15 '12 at 15:19
4 Answers
Look at java.util.concurrent.TimeUnit enum, you may use the units from here.
Don't reinvent the wheel.
For more complex time operation I would suggest Joda time library.

- 303,325
- 100
- 852
- 1,154

- 12,471
- 7
- 45
- 72
-
-
+1 for Joda - the problem with TimeUnit is that, as the name says, it only specifies the unit - the number of units must be specified separately. Joda's Period and Duration classes sound close to what @Paranaix is looking for. – GreyBeardedGeek Sep 15 '12 at 15:44
-
1@user3001: integer division may be simpler, but TimeUnit.MINUTES.toMillis(5) sure reads better and makes intent more clear. – Peter Štibraný Sep 15 '12 at 16:03
-
@PeterŠtibraný Its not obvious to me what 5 is in `toMillis(5)` Is it millis or nano-seconds or hour, it could return 0 for all I know ;) – Peter Lawrey Sep 15 '12 at 16:35
-
@PeterLawrey: it is (5) minutes to milliseconds, exactly as you read it :-) – Peter Štibraný Sep 16 '12 at 14:16
You can always reduce the resolution of a time e.g.
long millis = System.currentTimeMillis();
long seconds = millis / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
so if you want half hourly you can use
long halfHour = System.currentTimeMillis() / (30 * 60 * 1000L)

- 525,659
- 79
- 751
- 1,130
Granularity Units
If you meant simply a way to indicate a certain granularity of time such as hour versus minute versus second, the accepted Answer by Kremser is correct. The TimeUnit
enum is exactly this. But you have another choice also built into Java.
ChronoUnit
Note that Java 8 and later has a similar but longer list of values in java.time.temporal.ChronoUnit
versus the seven in TimeUnit
:
- FOREVER
- ERAS
- MILLENNIA
- CENTURIES
- DECADES
- YEARS
- MONTHS
- WEEKS
- DAYS
- HALF_DAYS
- HOURS
- MINUTES
- SECONDS
- MILLIS
- MICROS
- NANOS
Both enums, TimeUnit
& ChronoUnit
, are handy. Conversion between units is offered by TimeUnit
in various methods. ChronoUnit
calculates elapsed time with its between
method.
Clock increments
If you want the clock to tick in certain increments, the Clock
class offers alternate implementations.
Clock.tickSeconds
– Current time increments in whole seconds.Clock.tickMinutes
– Current time increments in whole minutes.Clock.tick
– Current time increments in an amount you specify
Pass your alternate Clock
implementation object as an optional argument in many java.time classes for use instead of the default clock.
Truncate values
If you have a date-time value and want to truncate it to drop smaller fields to zero, look at the truncatedTo
. You specify the granularity with the TemporalUnit
interface, with implementations found in ChronoUnit
.
For example, get the current moment, drop the fraction-of-second and seconds to get a value with whole minutes.
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( zoneId );
ZonedDateTime nowWholeMinutes = now.truncatedTo( ChronoUnit.MINUTES );

- 1
- 1

- 303,325
- 100
- 852
- 1,154
Use integer division, its simple:
long millis = System.currentTimeMillis();
will give you the current time in milliseconds.
If you want a time where the value would change only each resolution
milliseconds (e.g. each 1000 milliseconds), you can use integer division:
long resolution = 1000;
long result = millis / resolution * resolution
If you divide millis by resulution, you will get seconds in this example. /
does integer division, because both millis
and resolution
are integral variables. That would be the same as Math.floor(((float)millis) / ((float) resolution)))
. If you now multiply by resolution
, the result will only change if a new resolution unit is reached.
Example: Lets assume you want a clock of which the time only changes if a new 5 Minutes block is reached, so a block is e.g 0-4 minutes, 5-9 minutes, 10-15 minutes, etc.
5 Minutes are 1000 milliseconds / second * 60 seconds/ minute * 5 minutes = 300000 milliseconds.
So, if you set resolution = 300000
, result
will only change for each 5 Minute block once.
For millis = 0-299999, result=0. For millis=300000-599999, result=5, etc. You get the idea I guess.

- 3,437
- 5
- 28
- 54