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.
Joda Time also has a DateTimeConstants class with things like MILLIS_PER_SECOND, SECONDS_PER_MINUTE, MILLIS_PER_DAY, etc, etc.
If you mean to obtain the values Calendar
have all fields related to time management, with some simple reflection you can do
Field[] fields = Calendar.class.getFields();
for (Field f : fields)
{
String fName = f.toString();
System.out.println(fName.substring(fName.lastIndexOf('.')+1).replace("_", " ").toLowerCase());
}
this will output:
era
year
month
week of year
week of month
date
day of month
day of year
day of week
day of week in month
am pm
hour
hour of day
minute
second
millisecond
zone offset
dst offset
field count
sunday
monday
tuesday
wednesday
thursday
friday
saturday
january
february
march
april
may
june
july
august
september
october
november
december
undecimber
am
pm
all styles
short
long
from which you can exclude what you don't need.
If you need just constants you have them: Calendar.DAY_OF_MONTH
, Calendar.YEAR
and so on..
The Java TimeUnit seems to be what you want
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.
One more approach with already baked (for multiple call) Duration
instances (and 0 math operations):
ChronoUnit.DAYS.getDuration().getSeconds()
While TimeUnit
discussed in this Answer and Duration
discussed in this Answer probably more directly addresses the Question, there are some other handy units-of-time features in Java.
For units, see the ChronoUnit enum:
ERAS
MILLENNIA
CENTURIES
DECADES
YEARS
MONTHS
WEEKS
DAYS
HALF_DAYS
HOURS
MINUTES
SECONDS
MICROS
The java.time package has sophisticated enums for DayOfWeek and Month. So rather than pass around a mere number or string, you can pass full-fledged objects such as DayOfWeek.TUESDAY
or Month.FEBRUARY
.
The java.time framework also includes classes such as MonthDay
, YearMonth
, and Year
. Again, you can pass full-fledged objects rather than mere numbers or strings to make your code more self-documenting, ensure valid values, and provide type-safety.
TimeUnit
and ChronoUnit
We can easily convert between TimeUnit
and ChronoUnit
. See the new methods added to the older class, TimeUnit
.
The ThreeTen-Extra project provides additional classes to work with java.time. These include: DayOfMonth
, DayOfYear
, AmPm
, Quarter
, YearQuarter
, YearWeek
, Days
, Weeks
, Months
, Years
, and Interval
.