Package ucar.nc2.units
Class TimeUnit
- java.lang.Object
-
- ucar.nc2.units.SimpleUnit
-
- ucar.nc2.units.TimeUnit
-
@Immutable public class TimeUnit extends SimpleUnit
Handles Units that are time durations, eg in seconds, hours, days, years. It keeps track of the original unit name, rather than converting to canonical "seconds". Note this is a duration of time, not a date, seeCalendarDate
instead.
-
-
Field Summary
-
Fields inherited from class ucar.nc2.units.SimpleUnit
kmUnit, meterUnit, pressureUnit
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CalendarDate
add(CalendarDate cd)
Add the time amount to the given Date, return a new Date.double
convertTo(double value, TimeUnit outputUnit)
Convert given value of this unit to the new unit.static TimeUnit
create(double value, String unitString)
Constructor from a value and a unit name.static TimeUnit
create(String text)
Constructor from a String.boolean
equals(Object o)
TimeUnits with same value and unitString are equaldouble
getFactor()
Get the factor that converts this unit to seconds.String
getUnitString()
Get the "base" unit String, eg "secs" or "days"double
getValue()
Get the value.double
getValueInSeconds()
Get the time duration in seconds.int
hashCode()
TimeUnit
newValue(double value)
Create a new TimeUnit with the given value.TimeUnit
newValueInSeconds(double secs)
Create a new TimeUnit with the given value in seconds.String
toString()
String representation.-
Methods inherited from class ucar.nc2.units.SimpleUnit
convertTo, factory, factoryWithExceptions, getCanonicalString, getConversionFactor, getImplementingClass, isCompatible, isCompatible, isCompatibleWithExceptions, isDateUnit, isDateUnit, isTimeUnit, isTimeUnit, isUnknownUnit
-
-
-
-
Method Detail
-
create
public static TimeUnit create(String text) throws ucar.units.UnitException
Constructor from a String.- Parameters:
text
- [value]- Throws:
ucar.units.UnitException
- if bad format
-
create
public static TimeUnit create(double value, String unitString) throws ucar.units.UnitException
Constructor from a value and a unit name.- Parameters:
value
- amount of the unit.unitString
- Time unit string from udunits.- Throws:
ucar.units.UnitException
- if parse fails
-
getValue
public double getValue()
Get the value.- Overrides:
getValue
in classSimpleUnit
- Returns:
- value of this unit if ScaledUnit, else NaN
-
getFactor
public double getFactor()
Get the factor that converts this unit to seconds. getValueInSeconds = factor * value- Returns:
- factor that converts this unit to seconds.
-
newValue
public TimeUnit newValue(double value)
Create a new TimeUnit with the given value.
-
getUnitString
public String getUnitString()
Get the "base" unit String, eg "secs" or "days"- Overrides:
getUnitString
in classSimpleUnit
- Returns:
- unit string with no value
-
toString
public String toString()
String representation.- Overrides:
toString
in classSimpleUnit
-
getValueInSeconds
public double getValueInSeconds()
Get the time duration in seconds.- Returns:
- get current value in units of seconds
-
newValueInSeconds
public TimeUnit newValueInSeconds(double secs)
Create a new TimeUnit with the given value in seconds.
-
convertTo
public double convertTo(double value, TimeUnit outputUnit) throws ucar.units.ConversionException
Convert given value of this unit to the new unit. NOTE: the current value of this unit ignored, the given value is used instead. This is different than ucar.units or SimpleUnit.- Parameters:
value
- in the units of this "base unit"outputUnit
- convert to this base type, must be convertible to units of "seconds"- Returns:
- new value in the units of the "outputUnit
- Throws:
ucar.units.ConversionException
-
add
public CalendarDate add(CalendarDate cd)
Add the time amount to the given Date, return a new Date.- Parameters:
cd
- add to this CalendarDate- Returns:
- CalendarDate with getValueInSeconds() added to it.
-
equals
public boolean equals(Object o)
TimeUnits with same value and unitString are equal
-
-