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

3
  • 2
    What do you want to accomplish? Schedule a task for periodic execution? Commented 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 data Commented Sep 15, 2012 at 15:19
  • This post may be of interest to you: stackoverflow.com/questions/351565/… Commented Sep 15, 2012 at 15:19

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Integer division is much more simple
+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.
@user3001: integer division may be simpler, but TimeUnit.MINUTES.toMillis(5) sure reads better and makes intent more clear.
@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 ;)
@PeterLawrey: it is (5) minutes to milliseconds, exactly as you read it :-)
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) 

Comments

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 ); 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.