Package ucar.nc2.units
Class TimeUnit
- java.lang.Object
-
- ucar.nc2.units.SimpleUnit
-
- ucar.nc2.units.TimeUnit
-
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". The unit name never changes, but the value may.This is a wrapper around ucar.units. The underlying ucar.units.Unit always has a value of "1.0", ie is a base unit.
-
-
Field Summary
-
Fields inherited from class ucar.nc2.units.SimpleUnit
dateReferenceUnit, debugParse, kmUnit, meterUnit, pressureUnit, secsUnit, uu
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Date
add(Date d)
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.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.double
getValueInSeconds(double value)
Get the time duration in seconds of the specified value.int
hashCode()
Override hashcode to be consistent with equals.void
setValue(double value)
Set the value in the original units.void
setValueInSeconds(double secs)
Set the value, using the given number of seconds.String
toString()
String representation.-
Methods inherited from class ucar.nc2.units.SimpleUnit
convertTo, factory, factoryWithExceptions, getCanonicalString, getConversionFactor, getImplementingClass, getUnit, isCompatible, isCompatible, isCompatibleWithExceptions, isDateUnit, isDateUnit, isTimeUnit, isTimeUnit, isUnknownUnit, makeUnit
-
-
-
-
Constructor Detail
-
TimeUnit
public TimeUnit(String text) throws UnitException
Constructor from a String.- Parameters:
text
- [value]- Throws:
UnitException
- is bad format
-
TimeUnit
public TimeUnit(double value, String unitString) throws UnitException
Constructor from a value and a unit name.- Parameters:
value
- amount of the unit.unitString
- Time unit string from udunits.- Throws:
UnitException
- if parse fails
-
TimeUnit
public TimeUnit(TimeUnit src)
Copy Constructor.- Parameters:
src
- copy from here
-
-
Method Detail
-
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.
-
setValue
public void setValue(double value)
Set the value in the original units.- Parameters:
value
- set value, must be in units of this
-
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
-
getValueInSeconds
public double getValueInSeconds(double value)
Get the time duration in seconds of the specified value.- Parameters:
value
- convert this value, must be in units of this- Returns:
- get the value in units of seconds
-
setValueInSeconds
public void setValueInSeconds(double secs)
Set the value, using the given number of seconds.- Parameters:
secs
- : number of seconds; convert this to the units of this TimeUnit.
-
convertTo
public double convertTo(double value, TimeUnit outputUnit) throws 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:
ConversionException
-
add
public Date add(Date d)
Add the time amount to the given Date, return a new Date.- Parameters:
d
- add to this Date- Returns:
- Date with getValueInSeconds() added to it.
-
equals
public boolean equals(Object o)
TimeUnits with same value and unitString are equal
-
-