60 * 1000 miliseconds in 1 minute
60 seconds in 1 minute
1 minute in 1 minute
1/60 hours in 1 minute
1/(60*24) days in 1 minute
1/(60*24*365) years in 1 minute
1/(60*24*(365 * 4 + 1)) 4 years in 1 minute
* 60 is in 1 hour
* 60 * 24 is in 1 day
* 60 * 24 * 365 is in 1 year
etc.
Create them yourself, I guess, is the easiest. You can use the Date and Calendar classes to perform calculations with time and dates. Use the long data type to work with large numbers, such as miliseconds from 1 Januari 1970 UTC, System.currentTimeMillis().
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..
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:
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.
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.
Converting between TimeUnit and ChronoUnit
We can easily convert between TimeUnit and ChronoUnit. See the new methods added to the older class, TimeUnit.
The DayOfYear1 project provides additional classes to work with java.time. These include: DayOfMonth, DayOfYear, AmPm, Quarter, YearQuarter, YearWeek, Days, Weeks, Months, Years, and DayOfYear0.
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.