public class Time2 extends Object implements TimeIndex
TimeIndex
as an immutable object.
The design goals of Time2 are flexibility and performance. It is not
a replacement for java.util.Date
and is not a
competitor to Joda Time.
Time2 has no time zones, no daylight savings, no locales, no
Gregorian cutover. Dates before October 15 1582 do not correspond to
historical dates. The base of Time2 time is zero nanoseconds into
January 1st of year zero:
0000-01-01 00:00:00.000000000
It corresponds to the numerical time index 0L.
Warning note about time comparisons
This class implements Comparable
with simple semantics. Basically, it
considers "earlier" as "smaller". When time domain differs, times are
converted before performing the comparison. The conversion is done in the
following fashion:
compareTo
method.
Constructor and Description |
---|
Time2(TimeDomain domain,
long time)
Construct a time index with the given time domain and numerical time index.
|
Time2(TimeDomain domain,
long year,
int month,
int day,
int hour,
int min,
int sec,
int fsec,
Adjustment adjust)
Construct a time index with the given time domain and parameters.
|
Time2(TimeDomain domain,
String time)
Construct a time index with the given time domain and string.
|
Time2(TimeDomain domain,
String time,
Adjustment adjustment)
Construct a time index with the given time domain and string, with possible
adjustment.
|
Time2(TimeDomain domain,
TimeParts timeParts,
Adjustment adjust)
Constructor a time index from a time parts object with an adjustment
parameter.
|
Modifier and Type | Method and Description |
---|---|
TimeIndex |
add(long increment)
Return a new time index with the time incremented from this one.
|
long |
asLong()
Return the numeric index.
|
int |
asOffset()
Return the time as an offset from the origin.
|
int |
compareTo(TimeIndex otherTime) |
TimeIndex |
convert(TimeDomain domain)
Return a new time index by converting this one to the given time domain.
|
TimeIndex |
convert(TimeDomain domain,
Adjustment adjustment)
Return a new time index by converting this one to the given time domain,
possibly adjusting the time as specified.
|
boolean |
equals(Object obj) |
int |
getDay()
Return the day component of the time.
|
TimeIndex |
getDayByRank(Resolution basePeriod,
DayOfWeek day,
int rank)
Return the TimeIndex for a day defined by name and rank within a reference period.
|
DayOfWeek |
getDayOfWeek()
Return the day of the week.
|
int |
getFractionalSecond()
Return the sub-second component of the time.
|
int |
getHour()
Return the hour component of the time.
|
int |
getMinute()
Return the minute component of the time.
|
int |
getMonth()
Return the month component of the time.
|
int |
getSecond()
Return the second component of the time.
|
TimeDomain |
getTimeDomain()
Return the time domain.
|
long |
getYear()
Return the year component of the time.
|
int |
hashCode() |
long |
sub(TimeIndex time)
Return the number of time units since the time specified as argument.
|
String |
toString() |
String |
toString(String format)
Return a string representation of the time.
|
String |
toString(TimeFormatter formatter)
Return a string representation of the time.
|
public Time2(TimeDomain domain, long time)
domain
- a non-null time domaintime
- a valid numerical time indexpublic Time2(TimeDomain domain, long year, int month, int day, int hour, int min, int sec, int fsec, Adjustment adjust) throws T2Exception
See the comment in TimeDomain.time(String)
for important details about
discarding unnecessary time components.
domain
- a non-null time domainyear
- a year, which can be unusually large, depending on the domainmonth
- a number between 1 and 12day
- the day in the month, starting with 1hour
- an hour in the range 0-23min
- a minute in the range 0-59sec
- a second in the range 0-59fsec
- a fraction of a second in the current second in the range
0-999999999adjust
- a non-null allowed adjustment modeT2Exception
public Time2(TimeDomain domain, TimeParts timeParts, Adjustment adjust) throws T2Exception
domain
- the time domaintimeParts
- the time partsadjust
- an adjustmentT2Exception
- on failurepublic Time2(TimeDomain domain, String time, Adjustment adjustment) throws T2Exception
TimeScanner
defined in
the time domain.
If necessary the time is adjusted as allowed by the last argument.
See the comment in TimeDomain.time(String)
for important details about
discarding unnecessary time components.
domain
- a non-null time domaintime
- a string containing a representation of a date and timeadjustment
- a non-null allowed adjustment modeT2Exception
public Time2(TimeDomain domain, String time) throws T2Exception
TimeScanner
defined in the time domain.
No adjustment is allowed.
See the comment in TimeDomain.time(String)
for important details about
discarding unnecessary time components.
domain
- a non-null time domaintime
- a string containing a representation of a date and timeT2Exception
public TimeIndex convert(TimeDomain domain) throws T2Exception
TimeIndex
convert
in interface TimeIndex
domain
- a non-null time domainT2Exception
public TimeIndex convert(TimeDomain domain, Adjustment adjustment) throws T2Exception
TimeIndex
convert
in interface TimeIndex
domain
- a non-null time domainadjustment
- a non-null allowed adjustmentT2Exception
public TimeDomain getTimeDomain()
TimeIndex
getTimeDomain
in interface TimeIndex
public long asLong()
TimeIndex
public int asOffset() throws T2Exception
TimeIndex
asOffset
in interface TimeIndex
T2Exception
public TimeIndex add(long increment) throws T2Exception
TimeIndex
This method is related to sub(TimeIndex)
.
add
in interface TimeIndex
increment
- positive or negative number of units to addT2Exception
public long sub(TimeIndex time) throws T2Exception
TimeIndex
This method is related to add(long)
in the following way:
sub
in interface TimeIndex
time
- a non-null time index to substract from this time indexT2Exception
public long getYear()
TimeIndex
public int getMonth()
TimeIndex
public int getDay()
TimeIndex
public int getHour()
TimeIndex
public int getMinute()
TimeIndex
public int getSecond()
TimeIndex
public int getFractionalSecond()
TimeIndex
getFractionalSecond
in interface TimeIndex
public DayOfWeek getDayOfWeek() throws T2Exception
TimeIndex
getDayOfWeek
in interface TimeIndex
T2Exception
public TimeIndex getDayByRank(Resolution basePeriod, DayOfWeek day, int rank) throws T2Exception
TimeIndex
getDayByRank
in interface TimeIndex
basePeriod
- one of the values MONTH or YEARday
- the day namerank
- the day rankT2Exception
public String toString(TimeFormatter formatter)
TimeIndex
public String toString(String format)
TimeIndex
For example "%04d%02d%02d"
would produce 19700401 and
"%3$d.%2$d.%1$04d"
would produce 1.4.1970.
As a special trick, when the format string is empty, the date is returned in the compact format d.m.yy, with the day and month without a leading zero, and the year starting at the 3d digit.
If the argument is null, formatting is delegated to the default time formatter defined in the time factory.
In case of errors in the format string, an IllegalFormatException is thrown.
public int compareTo(TimeIndex otherTime)
compareTo
in interface Comparable<TimeIndex>
Copyright © 2017. All rights reserved.