Package org.postgresql.util
Class PGInterval
- java.lang.Object
-
- org.postgresql.util.PGobject
-
- org.postgresql.util.PGInterval
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class PGInterval extends PGobject implements java.io.Serializable, java.lang.Cloneable
This implements a class that handles the PostgreSQL interval type.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private intdaysprivate inthoursprivate booleanisNullprivate static intMICROS_IN_SECONDprivate intmicroSecondsprivate intminutesprivate intmonthsprivate intwholeSecondsprivate intyears
-
Constructor Summary
Constructors Constructor Description PGInterval()required by the driver.PGInterval(int years, int months, int days, int hours, int minutes, double seconds)Initializes all values of this interval to the specified values.PGInterval(java.lang.String value)Initialize a interval with a given interval string representation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(java.util.Calendar cal)Rolls this interval on a given calendar.voidadd(java.util.Date date)Rolls this interval on a given date.voidadd(PGInterval interval)Add this interval's value to the passed interval.java.lang.Objectclone()This must be overridden to allow the object to be cloned.booleanequals(java.lang.Object obj)Returns whether an object is equal to this one or not.intgetDays()Returns the days represented by this interval.intgetHours()Returns the hours represented by this interval.intgetMicroSeconds()intgetMinutes()Returns the minutes represented by this interval.intgetMonths()Returns the months represented by this interval.doublegetSeconds()Returns the seconds represented by this interval.java.lang.StringgetValue()Returns the stored interval information as a string.intgetWholeSeconds()intgetYears()Returns the years represented by this interval.inthashCode()Returns a hashCode for this object.private intlookAhead(java.lang.String value, int position, java.lang.String find)private static doublenullSafeDoubleGet(java.lang.String value)Returns double value of value or 0 if value is null.private static intnullSafeIntGet(java.lang.String value)Returns integer value of value or 0 if value is null.private voidparseISO8601Format(java.lang.String value)voidscale(int factor)Scale this interval by an integer factor.voidsetDays(int days)Set the days of this interval to the specified value.voidsetHours(int hours)Set the hours of this interval to the specified value.voidsetMinutes(int minutes)Set the minutes of this interval to the specified value.voidsetMonths(int months)Set the months of this interval to the specified value.voidsetSeconds(double seconds)Set the seconds of this interval to the specified value.voidsetValue(int years, int months, int days, int hours, int minutes, double seconds)Set all values of this interval to the specified values.voidsetValue(java.lang.String value)Sets a interval string represented value to this instance.voidsetYears(int years)Set the years of this interval to the specified value.
-
-
-
Field Detail
-
MICROS_IN_SECOND
private static final int MICROS_IN_SECOND
- See Also:
- Constant Field Values
-
years
private int years
-
months
private int months
-
days
private int days
-
hours
private int hours
-
minutes
private int minutes
-
wholeSeconds
private int wholeSeconds
-
microSeconds
private int microSeconds
-
isNull
private boolean isNull
-
-
Constructor Detail
-
PGInterval
public PGInterval()
required by the driver.
-
PGInterval
public PGInterval(java.lang.String value) throws java.sql.SQLExceptionInitialize a interval with a given interval string representation.- Parameters:
value- String representated interval (e.g. '3 years 2 mons')- Throws:
java.sql.SQLException- Is thrown if the string representation has an unknown format- See Also:
PGobject.setValue(String)
-
PGInterval
public PGInterval(int years, int months, int days, int hours, int minutes, double seconds)Initializes all values of this interval to the specified values.- Parameters:
years- yearsmonths- monthsdays- dayshours- hoursminutes- minutesseconds- seconds- See Also:
setValue(int, int, int, int, int, double)
-
-
Method Detail
-
lookAhead
private int lookAhead(java.lang.String value, int position, java.lang.String find)
-
parseISO8601Format
private void parseISO8601Format(java.lang.String value)
-
setValue
public void setValue(java.lang.String value) throws java.sql.SQLExceptionSets a interval string represented value to this instance. This method only recognize the format, that Postgres returns - not all input formats are supported (e.g. '1 yr 2 m 3 s').
-
setValue
public void setValue(int years, int months, int days, int hours, int minutes, double seconds)Set all values of this interval to the specified values.- Parameters:
years- yearsmonths- monthsdays- dayshours- hoursminutes- minutesseconds- seconds
-
getValue
public java.lang.String getValue()
Returns the stored interval information as a string.
-
getYears
public int getYears()
Returns the years represented by this interval.- Returns:
- years represented by this interval
-
setYears
public void setYears(int years)
Set the years of this interval to the specified value.- Parameters:
years- years to set
-
getMonths
public int getMonths()
Returns the months represented by this interval.- Returns:
- months represented by this interval
-
setMonths
public void setMonths(int months)
Set the months of this interval to the specified value.- Parameters:
months- months to set
-
getDays
public int getDays()
Returns the days represented by this interval.- Returns:
- days represented by this interval
-
setDays
public void setDays(int days)
Set the days of this interval to the specified value.- Parameters:
days- days to set
-
getHours
public int getHours()
Returns the hours represented by this interval.- Returns:
- hours represented by this interval
-
setHours
public void setHours(int hours)
Set the hours of this interval to the specified value.- Parameters:
hours- hours to set
-
getMinutes
public int getMinutes()
Returns the minutes represented by this interval.- Returns:
- minutes represented by this interval
-
setMinutes
public void setMinutes(int minutes)
Set the minutes of this interval to the specified value.- Parameters:
minutes- minutes to set
-
getSeconds
public double getSeconds()
Returns the seconds represented by this interval.- Returns:
- seconds represented by this interval
-
getWholeSeconds
public int getWholeSeconds()
-
getMicroSeconds
public int getMicroSeconds()
-
setSeconds
public void setSeconds(double seconds)
Set the seconds of this interval to the specified value.- Parameters:
seconds- seconds to set
-
add
public void add(java.util.Calendar cal)
Rolls this interval on a given calendar.- Parameters:
cal- Calendar instance to add to
-
add
public void add(java.util.Date date)
Rolls this interval on a given date.- Parameters:
date- Date instance to add to
-
add
public void add(PGInterval interval)
Add this interval's value to the passed interval. This is backwards to what I would expect, but this makes it match the other existing add methods.- Parameters:
interval- intval to add
-
scale
public void scale(int factor)
Scale this interval by an integer factor. The server can scale by arbitrary factors, but that would require adjusting the call signatures for all the existing methods like getDays() or providing our own justification of fractional intervals. Neither of these seem like a good idea without a strong use case.- Parameters:
factor- scale factor
-
nullSafeIntGet
private static int nullSafeIntGet(java.lang.String value) throws java.lang.NumberFormatExceptionReturns integer value of value or 0 if value is null.- Parameters:
value- integer as string value- Returns:
- integer parsed from string value
- Throws:
java.lang.NumberFormatException- if the string contains invalid chars
-
nullSafeDoubleGet
private static double nullSafeDoubleGet(java.lang.String value) throws java.lang.NumberFormatExceptionReturns double value of value or 0 if value is null.- Parameters:
value- double as string value- Returns:
- double parsed from string value
- Throws:
java.lang.NumberFormatException- if the string contains invalid chars
-
equals
public boolean equals(java.lang.Object obj)
Returns whether an object is equal to this one or not.
-
hashCode
public int hashCode()
Returns a hashCode for this object.
-
-