net.sourceforge.jtds.jdbc

Class DateTime


public class DateTime
extends java.lang.Object

Encapsulates Sybase date/time values and provides conversions to and from Java classes.
Version:
$Id: DateTime.java,v 1.4 2005/07/11 13:33:30 alin_sinpalean Exp $
Author:
Mike Hutchinson

Field Summary

(package private) static int
DATE_NOT_USED
Indicates date value not used.
(package private) static int
TIME_NOT_USED
Indicates time value not used.
private static ThreadLocal
calendar
Per thread instance of Calendar used for conversions.
private int
date
The date component of the server datetime value.
private Date
dateValue
Cached value of the datetime as a java.sql.Date.
private short
day
Unpacked day value.
private short
hour
Unpacked hour value.
private short
millis
Unpacked millisecond value.
private short
minute
Unpacked minute value.
private short
month
Unpacked month value.
private short
second
Unpacked second value.
private String
stringValue
Cached value of the datetime as a String.
private int
time
The time component of the server datetime value.
private Time
timeValue
Cached value of the datetime as a java.sql.Time.
private Timestamp
tsValue
Cached value of the datetime as a java.sql.Timestamp.
private boolean
unpacked
Indicates server datetime values have been unpacked.
private short
year
Unpacked year value.

Constructor Summary

DateTime(Date d)
Constructs a DateTime object from a java.sql.Date.
DateTime(Time t)
Constructs a DateTime object from a java.sql.Time.
DateTime(Timestamp ts)
Constructs a DateTime object from a java.sql.Timestamp.
DateTime(int date, int time)
Constructs a DateTime object from the two integer components of a datetime.
DateTime(short date, short time)
Constructs a DateTime object from the two short components of a smalldatetime.

Method Summary

(package private) int
getDate()
Retrieves the date component of a datetime value.
(package private) int
getTime()
Retrieves the time component of a datetime value.
void
packDate()
Converts a calendar date into days since 1900 (Sybase epoch).
void
packTime()
Converts separate time components into a datetime time value.
Date
toDate()
Retrieves the current datetime value as a Date.
Object
toObject()
Retrieves the current datetime value as a Time, Date or Timestamp.
String
toString()
Retrieves the current datetime value as a String.
Time
toTime()
Retrieves the current datetime value as a Time.
Timestamp
toTimestamp()
Retrieves the current datetime value as a Timestamp.
private void
unpackDateTime()
Converts a Julian datetime from the Sybase epoch of 1900-01-01 to the equivalent unpacked year/month/day etc.

Field Details

DATE_NOT_USED

(package private) static final int DATE_NOT_USED
Indicates date value not used.

TIME_NOT_USED

(package private) static final int TIME_NOT_USED
Indicates time value not used.

calendar

private static ThreadLocal calendar
Per thread instance of Calendar used for conversions.

date

private int date
The date component of the server datetime value.

dateValue

private Date dateValue
Cached value of the datetime as a java.sql.Date.

day

private short day
Unpacked day value.

hour

private short hour
Unpacked hour value.

millis

private short millis
Unpacked millisecond value.

minute

private short minute
Unpacked minute value.

month

private short month
Unpacked month value.

second

private short second
Unpacked second value.

stringValue

private String stringValue
Cached value of the datetime as a String.

time

private int time
The time component of the server datetime value.

timeValue

private Time timeValue
Cached value of the datetime as a java.sql.Time.

tsValue

private Timestamp tsValue
Cached value of the datetime as a java.sql.Timestamp.

unpacked

private boolean unpacked
Indicates server datetime values have been unpacked.

year

private short year
Unpacked year value.

Constructor Details

DateTime

(package private)  DateTime(Date d)
            throws SQLException
Constructs a DateTime object from a java.sql.Date.
Parameters:
d - Date object representing the datetime

DateTime

(package private)  DateTime(Time t)
Constructs a DateTime object from a java.sql.Time.
Parameters:
t - Time object representing the datetime

DateTime

(package private)  DateTime(Timestamp ts)
            throws SQLException
Constructs a DateTime object from a java.sql.Timestamp.
Parameters:
ts - Timestamp object representing the datetime

DateTime

(package private)  DateTime(int date,
                            int time)
Constructs a DateTime object from the two integer components of a datetime.
Parameters:
date - server date field
time - server time field

DateTime

(package private)  DateTime(short date,
                            short time)
Constructs a DateTime object from the two short components of a smalldatetime.
Parameters:
date - server date field
time - server time field

Method Details

getDate

(package private)  int getDate()
Retrieves the date component of a datetime value.
Returns:
the datetime date component as an int

getTime

(package private)  int getTime()
Retrieves the time component of a datetime value.
Returns:
the datetime time component as an int

packDate

public void packDate()
            throws SQLException
Converts a calendar date into days since 1900 (Sybase epoch).

Algorithm from Fliegel, H F and van Flandern, T C (1968). Communications of the ACM, Vol 11, No 10 (October, 1968).

           INTEGER FUNCTION JD (YEAR,MONTH,DAY)
     C
     C---COMPUTES THE JULIAN DATE (JD) GIVEN A GREGORIAN CALENDAR
     C   DATE (YEAR,MONTH,DAY).
     C
           INTEGER YEAR,MONTH,DAY,I,J,K
     C
           I= YEAR
           J= MONTH
           K= DAY
     C
           JD= K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)
          2    /12-3*((I+4900+(J-14)/12)/100)/4
     C
           RETURN
           END
 

packTime

public void packTime()
Converts separate time components into a datetime time value.

toDate

public Date toDate()
Retrieves the current datetime value as a Date.
Returns:
the current datetime value as a java.sql.Date

toObject

public Object toObject()
Retrieves the current datetime value as a Time, Date or Timestamp.
Returns:
the current datetime value as an java.lang.Object

toString

public String toString()
Retrieves the current datetime value as a String.
Returns:
the current datetime value as a String

toTime

public Time toTime()
Retrieves the current datetime value as a Time.
Returns:
the current datetime value as a java.sql.Time

toTimestamp

public Timestamp toTimestamp()
Retrieves the current datetime value as a Timestamp.
Returns:
the current datetime value as a java.sql.Timestamp

unpackDateTime

private void unpackDateTime()
Converts a Julian datetime from the Sybase epoch of 1900-01-01 to the equivalent unpacked year/month/day etc. Algorithm from Fliegel, H F and van Flandern, T C (1968). Communications of the ACM, Vol 11, No 10 (October, 1968).
           SUBROUTINE GDATE (JD, YEAR,MONTH,DAY)
     C
     C---COMPUTES THE GREGORIAN CALENDAR DATE (YEAR,MONTH,DAY)
     C   GIVEN THE JULIAN DATE (JD).
     C
           INTEGER JD,YEAR,MONTH,DAY,I,J,K
     C
           L= JD+68569
           N= 4*L/146097
           L= L-(146097*N+3)/4
           I= 4000*(L+1)/1461001
           L= L-1461*I/4+31
           J= 80*L/2447
           K= L-2447*J/80
           L= J/11
           J= J+2-12*L
           I= 100*(N-49)+I+L
     C
           YEAR= I
           MONTH= J
           DAY= K
     C
           RETURN
           END
 

Generated on September 18 2009