Is there a Java package with all the annoying time constants like milliseconds/seconds/minutes in a minute/hour/day/year? I'd hate to duplicate something like that.
A runtime constant is a value that is computed only while the program is running. If you run the same program more than once: A compile-time constant will have the same value each time the application is run. A runtime constant can have a different value each time the application is run.
static Calendar working; static { working = GregorianCalendar. getInstance(); working. set(1776, 6, 4, 0, 0, 1); } public static final Calendar beforeFirstCalendar = working; public static final Date beforeFirstDate = working. getTime();
One example of a constant is pi, because it always has the same value, which is 3.14159… This concept of constants is relevant to us, because we often need to declare constants when we write programs in Java.
public static final variables are also known as a compile-time constant, the public is optional there. They are replaced with actual values at compile time because compiler know their value up-front and also knows that it cannot be changed during run-time.
I would go with java TimeUnit if you are not including joda-time in your project already. You don't need to include an external lib and it is fairly straightforward.
Whenever you need those "annoying constants" you usually need them to mutliply some number for cross-unit conversion. Instead you can use TimeUnit to simply convert the values without explicit multiplication.
This:
long millis = hours * MINUTES_IN_HOUR * SECONDS_IN_MINUTE * MILLIS_IN_SECOND;
becomes this:
long millis = TimeUnit.HOURS.toMillis(hours);
If you expose a method that accepts some value in, say, millis and then need to convert it, it is better to follow what java concurrency API does:
public void yourFancyMethod(long somePeriod, TimeUnit unit) { int iNeedSeconds = unit.toSeconds(somePeriod); }
If you really need the constants very badly you can still get i.e. seconds in an hour by calling:
int secondsInHour = TimeUnit.HOURS.toSeconds(1);
As an alternative to TimeUnit
, you might for some reason prefer the Duration class from java.time package:
Duration.ofDays(1).getSeconds() // returns 86400; Duration.ofMinutes(1).getSeconds(); // 60 Duration.ofHours(1).toMinutes(); // also 60 //etc.
Additionally, if you would go deeper and have analyzed how Duration.ofDays(..) method works, you would see the following code:
return create(Math.multiplyExact(days, SECONDS_PER_DAY), 0);
where SECONDS_PER_DAY
is a package protected static constant from LocalTime class.
/** * Seconds per day. */ static final int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY; //there are also many others, like HOURS_PER_DAY, MINUTES_PER_HOUR, etc.
I think it is safe to assume that if there would be any package, which would defined "all the annoying time constants like miliseconds/seconds/minutes" as you call them, I believe Java SDK Developers would have use them.
Why are this LocalTime
constants package protected and not public that is a good question, I believe there is a reason for that. For now it looks like you really have to copy them and maintain on your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With