home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / sql / Timestamp.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  8.4 KB  |  318 lines

  1. /*
  2.  * @(#)Timestamp.java    1.17 98/03/18
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.sql;
  16.  
  17. /**
  18.  * <P>This class is a thin wrapper around java.util.Date that allows
  19.  * JDBC to identify this as a SQL TIMESTAMP value. It adds the ability
  20.  * to hold the SQL TIMESTAMP nanos value and provides formatting and
  21.  * parsing operations to support the JDBC escape syntax for timestamp
  22.  * values.
  23.  *
  24.  * <P><B>Note:</B> This type is a composite of a java.util.Date and a
  25.  * separate nanos value. Only integral seconds are stored in the
  26.  * java.util.Date component. The fractional seconds - the nanos - are
  27.  * separate. The getTime method will only return integral seconds. If
  28.  * a time value that includes the fractional seconds is desired you
  29.  * must convert nanos to milliseconds (nanos/1000000) and add this to
  30.  * the getTime value. Also note that the hashcode() method uses the
  31.  * underlying java.util.Data implementation and therefore does not
  32.  * include nanos in its computation.  
  33.  */
  34. public class Timestamp extends java.util.Date {
  35.  
  36.     /**
  37.      * Construct a Timestamp Object
  38.      *
  39.      * @param year year-1900
  40.      * @param month 0 to 11 
  41.      * @param day 1 to 31
  42.      * @param hour 0 to 23
  43.      * @param minute 0 to 59
  44.      * @param second 0 to 59
  45.      * @param nano 0 to 999,999,999
  46.      * @deprecated
  47.      */
  48.     public Timestamp(int year, int month, int date, 
  49.              int hour, int minute, int second, int nano) {
  50.     super(year, month, date, hour, minute, second);
  51.     if (nano > 999999999 || nano < 0) {
  52.         throw new IllegalArgumentException("nanos > 999999999 or < 0");
  53.     }
  54.     nanos = nano;
  55.     }
  56.  
  57.     /**
  58.      * Construct a Timestamp using a milliseconds time value. The
  59.      * integral seconds are stored in the underlying date value; the
  60.      * fractional seconds are stored in the nanos value.
  61.      *
  62.      * @param time milliseconds since January 1, 1970, 00:00:00 GMT 
  63.      */
  64.     public Timestamp(long time) {
  65.     super((time/1000)*1000);
  66.     nanos = (int)((time%1000) * 1000000);
  67.     if (nanos < 0) {
  68.         nanos = 1000000000 + nanos;        
  69.         setTime(((time/1000)-1)*1000);
  70.     }
  71.     }
  72.  
  73.     private int nanos;
  74.  
  75.     /**
  76.      * Convert a string in JDBC timestamp escape format to a Timestamp value
  77.      *
  78.      * @param s timestamp in format "yyyy-mm-dd hh:mm:ss.fffffffff"
  79.      * @return corresponding Timestamp
  80.      */
  81.     public static Timestamp valueOf(String s) {
  82.     String date_s;
  83.     String time_s;
  84.     String nanos_s;
  85.     int year;
  86.     int month;
  87.     int day;
  88.     int hour;
  89.     int minute;
  90.     int second;
  91.     int a_nanos = 0;
  92.     int firstDash;
  93.     int secondDash;
  94.     int dividingSpace;
  95.     int firstColon = 0;
  96.     int secondColon = 0;
  97.     int period = 0;
  98.     String formatError = "Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
  99.     String zeros = "000000000";
  100.  
  101.     if (s == null) throw new java.lang.IllegalArgumentException("null string");
  102.  
  103.     // Split the string into date and time components
  104.     s = s.trim();
  105.     dividingSpace = s.indexOf(' ');
  106.     if (dividingSpace > 0) {
  107.         date_s = s.substring(0,dividingSpace);
  108.         time_s = s.substring(dividingSpace+1);
  109.     } else {
  110.         throw new java.lang.IllegalArgumentException(formatError);
  111.     }
  112.  
  113.  
  114.     // Parse the date
  115.     firstDash = date_s.indexOf('-');
  116.     secondDash = date_s.indexOf('-', firstDash+1);
  117.  
  118.     // Parse the time
  119.     if (time_s == null) 
  120.         throw new java.lang.IllegalArgumentException(formatError);
  121.     firstColon = time_s.indexOf(':');
  122.     secondColon = time_s.indexOf(':', firstColon+1);
  123.     period = time_s.indexOf('.', secondColon+1);
  124.  
  125.     // Convert the date
  126.     if ((firstDash > 0) & (secondDash > 0) & 
  127.         (secondDash < date_s.length()-1)) {
  128.         year = Integer.parseInt(date_s.substring(0, firstDash)) - 1900;
  129.         month = 
  130.         Integer.parseInt(date_s.substring
  131.                  (firstDash+1, secondDash)) - 1;
  132.         day = Integer.parseInt(date_s.substring(secondDash+1));
  133.     } else {        
  134.         throw new java.lang.IllegalArgumentException(formatError);
  135.     }
  136.  
  137.     // Convert the time; default missing nanos
  138.     if ((firstColon > 0) & (secondColon > 0) & 
  139.         (secondColon < time_s.length()-1)) {
  140.         hour = Integer.parseInt(time_s.substring(0, firstColon));
  141.         minute = 
  142.         Integer.parseInt(time_s.substring(firstColon+1, secondColon));
  143.         if ((period > 0) & (period < time_s.length()-1)) {
  144.         second = 
  145.             Integer.parseInt(time_s.substring(secondColon+1, period));
  146.         nanos_s = time_s.substring(period+1);
  147.         if (nanos_s.length() > 9) 
  148.             throw new java.lang.IllegalArgumentException(formatError);
  149.         if (!Character.isDigit(nanos_s.charAt(0)))
  150.             throw new java.lang.IllegalArgumentException(formatError);
  151.         nanos_s = nanos_s + zeros.substring(0,9-nanos_s.length());
  152.         a_nanos = Integer.parseInt(nanos_s);
  153.         } else if (period > 0) {
  154.         throw new java.lang.IllegalArgumentException(formatError);
  155.         } else {
  156.         second = Integer.parseInt(time_s.substring(secondColon+1));
  157.         }
  158.     } else {
  159.         throw new java.lang.IllegalArgumentException();
  160.     }
  161.  
  162.     return new Timestamp(year, month, day, hour, minute, second, a_nanos);
  163.     }
  164.  
  165.     /**
  166.      * Format a timestamp in JDBC timestamp escape format
  167.      *
  168.      * @return a String in yyyy-mm-dd hh:mm:ss.fffffffff format
  169.      */
  170.     public String toString () {
  171.     int year = super.getYear() + 1900;
  172.     int month = super.getMonth() + 1;
  173.     int day = super.getDate();
  174.     int hour = super.getHours();
  175.     int minute = super.getMinutes();
  176.     int second = super.getSeconds();
  177.     String yearString;
  178.     String monthString;
  179.     String dayString;
  180.     String hourString;
  181.     String minuteString;
  182.     String secondString;
  183.     String nanosString;
  184.     String zeros = "000000000";
  185.  
  186.         
  187.     yearString = "" + year;
  188.     if (month < 10) {
  189.         monthString = "0" + month;
  190.     } else {        
  191.         monthString = Integer.toString(month);
  192.     }
  193.     if (day < 10) {
  194.         dayString = "0" + day;
  195.     } else {        
  196.         dayString = Integer.toString(day);
  197.     }
  198.     if (hour < 10) {
  199.         hourString = "0" + hour;
  200.     } else {        
  201.         hourString = Integer.toString(hour);
  202.     }
  203.     if (minute < 10) {
  204.         minuteString = "0" + minute;
  205.     } else {        
  206.         minuteString = Integer.toString(minute);
  207.     }
  208.     if (second < 10) {
  209.         secondString = "0" + second;
  210.     } else {        
  211.         secondString = Integer.toString(second);
  212.     }
  213.     if (nanos == 0) {
  214.         nanosString = "0";
  215.     } else {
  216.         nanosString = Integer.toString(nanos);
  217.  
  218.         // Add leading zeros
  219.         nanosString = zeros.substring(0,(9-nanosString.length())) + 
  220.         nanosString;
  221.         
  222.         // Truncate trailing zeros
  223.         char[] nanosChar = new char[nanosString.length()];
  224.         nanosString.getChars(0, nanosString.length(), nanosChar, 0);
  225.         int truncIndex = 8;        
  226.         while (nanosChar[truncIndex] == '0') {
  227.         truncIndex--;
  228.         }
  229.         nanosString = new String(nanosChar,0,truncIndex+1);
  230.     }
  231.     
  232.     return (yearString + "-" + monthString + "-" + dayString + " " + 
  233.         hourString + ":" + minuteString + ":" + secondString + "."
  234.                 + nanosString);
  235.     }
  236.  
  237.     /**
  238.      * Get the Timestamp's nanos value
  239.      *
  240.      * @return the Timestamp's fractional seconds component
  241.      */
  242.     public int getNanos() {
  243.     return nanos;
  244.     }
  245.  
  246.     /**
  247.      * Set the Timestamp's nanos value
  248.      *
  249.      * @param n the new fractional seconds component
  250.      */
  251.     public void setNanos(int n) {
  252.     if (n > 999999999 || n < 0) {
  253.         throw new IllegalArgumentException("nanos > 999999999 or < 0");
  254.     }
  255.     nanos = n;
  256.     }
  257.  
  258.     /**
  259.      * Test Timestamp values for equality
  260.      *
  261.      * @param ts the Timestamp value to compare with
  262.      */
  263.     public boolean equals(Timestamp ts) {
  264.     if (super.equals(ts)) {
  265.         if (nanos == ts.nanos) {
  266.         return true;
  267.         } else {
  268.         return false;
  269.         }
  270.     } else {
  271.         return false;
  272.     }
  273.     }
  274.  
  275.     /**
  276.      * Is this timestamp earlier than the timestamp argument?
  277.      *
  278.      * @param ts the Timestamp value to compare with
  279.      */
  280.     public boolean before(Timestamp ts) {
  281.     if (super.before(ts)) {
  282.         return true;
  283.     } else {
  284.         if (super.equals(ts)) {
  285.         if (nanos < ts.nanos) {
  286.             return true;
  287.         } else {
  288.             return false;
  289.         }
  290.         } else {
  291.         return false;
  292.         }
  293.     }
  294.     }
  295.  
  296.     /**
  297.      * Is this timestamp later than the timestamp argument?
  298.      *
  299.      * @param ts the Timestamp value to compare with
  300.      */
  301.     public boolean after(Timestamp ts) {
  302.     if (super.after(ts)) {
  303.         return true;
  304.     } else {
  305.         if (super.equals(ts)) {
  306.         if (nanos > ts.nanos) {
  307.             return true;
  308.         } else {
  309.             return false;
  310.         }
  311.         } else {
  312.         return false;
  313.         }
  314.     }
  315.     }
  316. }
  317.  
  318.