home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Date.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  164 lines

  1. /*
  2.  * @(#)Date.java    1.5 96/11/23
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.sql;
  24.  
  25. /**
  26.  * <P>This class is a thin wrapper around java.util.Date that allows
  27.  * JDBC to identify this as a SQL DATE value. It adds formatting and
  28.  * parsing operations to support the JDBC escape syntax for date
  29.  * values.
  30.  */
  31. public class Date extends java.util.Date {
  32.  
  33.     /**
  34.      * Construct a Date  
  35.      *
  36.      * @param year year-1900
  37.      * @param month 0 to 11 
  38.      * @param day 1 to 31
  39.      */
  40.     public Date(int year, int month, int day) {
  41.     super(year, month, day);
  42.     }
  43.  
  44.     /**
  45.      * Construct a Date using a milliseconds time value
  46.      *
  47.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  48.      */
  49.     public Date(long date) {
  50.     // If the millisecond date value contains time info, mask it out.
  51.     super(date);
  52.     int year = getYear();
  53.     int month = getMonth();
  54.     int day = getDate();
  55.     super.setTime(0);
  56.     setYear(year);
  57.     setMonth(month);
  58.     setDate(day);
  59.     }
  60.  
  61.     /**
  62.      * Set a Date using a milliseconds time value
  63.      *
  64.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  65.      */
  66.     public void setTime(long date) {
  67.     // If the millisecond date value contains time info, mask it out.
  68.     super.setTime(date);
  69.     int year = getYear();
  70.     int month = getMonth();
  71.     int day = getDate();
  72.     super.setTime(0);
  73.     setYear(year);
  74.     setMonth(month);
  75.     setDate(day);
  76.     }
  77.  
  78.     /**
  79.      * Convert a string in JDBC date escape format to a Date value
  80.      *
  81.      * @param s date in format "yyyy-mm-dd"
  82.      * @return corresponding Date
  83.      */
  84.     public static Date valueOf(String s) {
  85.     int year;
  86.     int month;
  87.     int day;
  88.     int firstDash;
  89.     int secondDash;
  90.  
  91.     if (s == null) throw new java.lang.IllegalArgumentException();
  92.  
  93.     firstDash = s.indexOf('-');
  94.     secondDash = s.indexOf('-', firstDash+1);
  95.     if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  96.         year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  97.         month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  98.         day = Integer.parseInt(s.substring(secondDash+1));     
  99.     } else {
  100.         throw new java.lang.IllegalArgumentException();
  101.     }
  102.             
  103.     return new Date(year, month, day);
  104.     }
  105.  
  106.     /**
  107.      * Format a date in JDBC date escape format  
  108.      *
  109.      * @return a String in yyyy-mm-dd format
  110.      */
  111.     public String toString () {
  112.     int year = super.getYear() + 1900;
  113.     int month = super.getMonth() + 1;
  114.     int day = super.getDate();
  115.     String yearString;
  116.     String monthString;
  117.     String dayString;
  118.  
  119.         
  120.     yearString = Integer.toString(year);
  121.  
  122.     if (month < 10) {
  123.         monthString = "0" + month;
  124.     } else {        
  125.         monthString = Integer.toString(month);
  126.     }
  127.  
  128.     if (day < 10) {
  129.         dayString = "0" + day;
  130.     } else {        
  131.         dayString = Integer.toString(day);
  132.     }
  133.  
  134.     return ( yearString + "-" + monthString + "-" + dayString);
  135.     }
  136.  
  137.     // Override all the time operations inherited from java.util.Date;
  138.     public int getHours() {
  139.     throw new java.lang.IllegalArgumentException();
  140.     }
  141.  
  142.     public int getMinutes() {
  143.     throw new java.lang.IllegalArgumentException();
  144.     }
  145.     
  146.     public int getSeconds() {
  147.     throw new java.lang.IllegalArgumentException();
  148.     }
  149.  
  150.     public void setHours(int i) {
  151.     throw new java.lang.IllegalArgumentException();
  152.     }
  153.  
  154.     public void setMinutes(int i) {
  155.     throw new java.lang.IllegalArgumentException();
  156.     }
  157.  
  158.     public void setSeconds(int i) {
  159.     throw new java.lang.IllegalArgumentException();
  160.     }
  161.  
  162. }
  163.  
  164.