views:

65

answers:

5

Is there a Java package with all the annoying time consts , like miliseconds/seconds/minutes in a minute / hour /day / year ? I'd hate to duplicate something like that

+1  A: 
              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().

Pindatjuh
+4  A: 

Joda Time contains classes such as Days, which contain methods such as toStandardSeconds(). So you can write:

int seconds = Days.ONE.toStandardSeconds();

although it seems a little verbose, and perhaps is only useful for more complex scenarios such as leap years etc.

Brian Agnew
Thanks! I just love Joda :)
yossale
A: 

I doubt it, because they aren't all constants. The number of milliseconds for a year varies, right?

duffymo
Yes, you're right. http://en.wikipedia.org/wiki/Leap_year
Pindatjuh
duffymo: You're right , of course , but for our needs , 60 seconds in a minute is a good enough approximation
yossale
A: 

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

Jack
A: 

The Java TimeUnit seems to be what you want

Martin