time - Is there a global variable in Java for the length of day? -
i don't want write 24 * 60 * 60 * 1000
in code instead use more readable static day
variable.
i know can myself define variable writing
private static final long day = 24*60*60*1000;
however seems me basic function. covered in standard library somewhere?
you try duration api of java.time
package in java 8 java.time.duration.ofdays(1).getseconds()
.
just store in public static field:
public static final long seconds_in_day = duration.ofdays(1).getseconds()
your method of interest ofdays(long)
method:
obtains duration representing number of standard 24 hour days. seconds calculated based on standard definition of day, each day 86400 seconds implies 24 hour day. nanosecond in second field set zero.
parameters: days - number of days, positive or negative
returns: duration, not null
throws: arithmeticexception - if input days exceeds capacity of duration
follow call getseconds()
method on same duration api:
gets number of seconds in duration. length of duration stored using 2 fields - seconds , nanoseconds. nanoseconds part value 0 999,999,999 adjustment length in seconds. total duration defined calling method , getnano().
a duration represents directed distance between 2 points on time-line. negative duration expressed negative sign of seconds part. duration of -1 nanosecond stored -1 seconds plus 999,999,999 nanoseconds.
returns: whole seconds part of length of duration, positive or negative
Comments
Post a Comment