Java consts time

Is there a Java package with all the annoying time constants like milliseconds / seconds / minutes per minute / hour / day / year? I would not want to duplicate something like that.

+63
java date time constants
Mar 14
source share
12 answers

Joda-Time contains classes such as Days that contain methods such as toStandardSeconds () . Therefore, you can write:

int seconds = Days.ONE.toStandardSeconds(); 

although this seems a bit verbose and perhaps only useful for more complex scenarios such as leap years, etc.

+22
Mar 14
source share

I would go with java TimeUnit if you hadn't included joda-time in your project yet. You do not need to include an external library, and it is quite simple.

Whenever you need these "annoying constants", you usually need them to change some numbers for conversion between units. Instead, you can use TimeUnit to easily convert values ​​without explicit multiplication.

It:

 long milis = hours * MINUTES_IN_HOUR * SECONDS_IN_MINUTE * MILLIS_IN_SECOND; 

becomes the following:

 long milis = TimeUnit.HOURS.toMillis(hours); 

If you expose a method that takes a value in, for example, millis, and then you need to convert it, it is better to follow what the java concurrency API does:

 public void yourFancyMethod(long somePeriod, TimeUnit unit) { int iNeedSeconds = unit.toSeconds(somePeriod); } 

If you really need constants very badly, you can still get ie seconds per hour by calling:

 int secondsInHour = TimeUnit.HOURS.toSeconds(1); 
+115
Mar 06 '13 at 18:51
source share

If on android I suggest:

android.text.format.DateUtils

 DateUtils.SECOND_IN_MILLIS DateUtils.MINUTE_IN_MILLIS DateUtils.HOUR_IN_MILLIS DateUtils.DAY_IN_MILLIS DateUtils.WEEK_IN_MILLIS DateUtils.YEAR_IN_MILLIS 
+34
Mar 30 '14 at 11:18
source share

Java 8 / java.time solution

As an alternative to TimeUnit for some reason you might prefer the Duration class from the java.time package:

 Duration.ofDays(1).getSeconds() // returns 86400; Duration.ofMinutes(1).getSeconds(); // 60 Duration.ofHours(1).toMinutes(); // also 60 //etc. 

In addition, if you go deeper and analyze how the Duration.ofDays (..) method works, you will see the following code:

 return create(Math.multiplyExact(days, SECONDS_PER_DAY), 0); 

where SECONDS_PER_DAY is the static constant protected by the package from the 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 is some package that defines "all annoying time constants, such as miliseconds / seconds / minutes", as you call them, I believe that the Java SDK developers would use them .

Why is this package of LocalTime constants protected and not publicly available, this is a good question, I think there is a reason for this. At the moment, it looks like you really need to copy them and maintain them yourself.

+27
Feb 01 '16 at 10:26
source share

Java TimeUnit seems to be what you want

+14
Mar 14 '10 at 15:21
source share

Joda Time also has a DateTimeConstants class with things like MILLIS_PER_SECOND, SECONDS_PER_MINUTE, MILLIS_PER_DAY, etc. etc.

+11
Oct. 10 '13 at 13:10
source share

While TimeUnit discussed in this answer and Duration , discussed in this answer , perhaps more directly addresses the Question, in Java there are some other convenient functions of units of time.

java.time

The java.time package contains complex enumerations for DayOfWeek and Month . Therefore, instead of going through a prime number or a string, you can pass full-featured 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 complete objects, not just numbers or strings, to make your code more self-documenting, provide valid values, and ensure type safety.

ThreeTen-Extra Project

The ThreeTen-Extra project provides additional classes for working with java.time. These include: DayOfMonth , DayOfYear , AmPm , Quarter , YearQuarter , YearWeek , Days , Weeks , Months , Years and Interval .

+2
Jul 31 '14 at 19:17
source share

Here is what I use to get milliseconds.

 import javax.management.timer.Timer; Timer.ONE_HOUR Timer.ONE_DAY Timer.ONE_MINUTE Timer.ONE_SECOND Timer.ONE_WEEK 
+2
Jul 18 '17 at 5:11
source share

Another approach with already baked (for multiple calls) Duration instances (and 0 math operations):

 ChronoUnit.DAYS.getDuration().getSeconds() 
+2
Nov 23 '17 at 10:05
source share
  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 think, 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 milliseconds from January 1, 1970 UTC, System.currentTimeMillis() .

0
Mar 14
source share

I doubt it because they are not all constants. The number of milliseconds per year is changing, right?

0
Mar 14
source share

If you want to get Calendar values, 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 do not need.

If you need only constants, you need them: Calendar.DAY_OF_MONTH , Calendar.YEAR , etc.

0
Mar 14
source share



All Articles