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– João Silva2012-09-15 15:15:33 +00:00Commented Sep 15, 2012 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 dataSebastian Hoffmann– Sebastian Hoffmann2012-09-15 15:19:10 +00:00Commented Sep 15, 2012 at 15:19
- This post may be of interest to you: stackoverflow.com/questions/351565/…BlackVegetable– BlackVegetable2012-09-15 15:19:12 +00:00Commented Sep 15, 2012 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.
5 Comments
toMillis(5) Is it millis or nano-seconds or hour, it could return 0 for all I know ;)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 ); Comments
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.