2

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. ?

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77

4 Answers4

8

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
2

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)
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

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.

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 );
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-1

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.

user3001
  • 3,437
  • 5
  • 28
  • 54