Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hsqldb/lib/HsqlDateTime.java


1   /* Copyright (c) 2001-2002, The HSQL Development Group
2    * All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    *
7    * Redistributions of source code must retain the above copyright notice, this
8    * list of conditions and the following disclaimer.
9    *
10   * Redistributions in binary form must reproduce the above copyright notice,
11   * this list of conditions and the following disclaimer in the documentation
12   * and/or other materials provided with the distribution.
13   *
14   * Neither the name of the HSQL Development Group nor the names of its
15   * contributors may be used to endorse or promote products derived from this
16   * software without specific prior written permission.
17   *
18   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21   * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG, 
22   * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
23   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
24   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  
31  
32  package org.hsqldb.lib;
33  
34  import java.sql.SQLException;
35  import java.sql.Date;
36  import java.sql.Time;
37  import java.sql.Timestamp;
38  import java.text.SimpleDateFormat;
39  import java.util.Calendar;
40  
41  // fredt@users 20020130 - patch 1.7.0 by fredt - new class
42  // replaces patch by deforest@users
43  // fredt@users 20020414 - patch 517028 by peterhudson@users - use of calendar
44  
45  /**
46   *  collection of static methods to convert Date, Time and Timestamp strings
47   *  into corresponding Java objects. Also accepts SQL literals such as NOW,
48   *  TODAY as valid strings and returns the current date / time / datetime.
49   *  Compatible with jdk 1.1.x
50   *
51   * @author  fredt@users
52   * @version 1.7.0
53   */
54  public class HsqlDateTime {
55  
56      /**
57       *  Converts a string in JDBC timestamp escape format to a
58       *  <code>Timestamp</code> value.
59       *
60       * @param s timestamp in format <code>yyyy-mm-dd hh:mm:ss.fffffffff</code>
61       *      where end part can be omitted, or "NOW" (case insensitive)
62       * @return  corresponding <code>Timestamp</code> value
63       * @exception java.lang.IllegalArgumentException if the given argument
64       * does not have the format <code>yyyy-mm-dd hh:mm:ss.fffffffff</code>
65       */
66      public static Timestamp timestampValue(String s) {
67  
68          if (s == null) {
69              throw new java.lang.IllegalArgumentException("null string");
70          }
71  
72          if (s.toUpperCase().equals("NOW")
73                  || s.toUpperCase().equals("CURRENT_TIMESTAMP")) {
74              return new Timestamp(System.currentTimeMillis());
75          }
76  
77          final String zerodatetime = "1970-01-01 00:00:00.000000000";
78  
79          s = s + zerodatetime.substring(s.length());
80  
81          return Timestamp.valueOf(s);
82      }
83  
84      /**
85       * @param  time milliseconds
86       * @param  nano nanoseconds
87       * @return  Timestamp object
88       */
89      public static Timestamp timestampValue(long time, int nano) {
90  
91          Timestamp ts = new Timestamp(time);
92  
93          ts.setNanos(nano);
94  
95          return ts;
96      }
97  
98      /**
99       *  Converts a string in JDBC date escape format to a <code>Date</code>
100      *  value. Also accepts Timestamp values.
101      *
102      * @param s date in format <code>yyyy-mm-dd</code>,
103      *  'TODAY', 'NOW', 'CURRENT_DATE', 'SYSDATE' (case independent)
104      * @return  corresponding <code>Date</code> value
105      * @exception java.lang.IllegalArgumentException if the given argument
106      * does not have the format <code>yyyy-mm-dd</code>
107      */
108     public static Date dateValue(String s) {
109 
110         if (s == null) {
111             throw new java.lang.IllegalArgumentException("null string");
112         }
113 
114         s = s.toUpperCase();
115 
116         if (s.equals("TODAY") || s.equals("NOW") || s.equals("CURRENT_DATE")
117                 || s.equals("SYSDATE")) {
118             return new Date(System.currentTimeMillis());
119         }
120 
121         return Date.valueOf(s.substring(0, sdfdPattern.length()));
122     }
123 
124     /**
125      * Converts a string in JDBC date escape format to a
126      * <code>Time</code> value.
127      *
128      * @param s date in format <code>hh:mm:ss</code>
129      * 'CURRENT_TIME' or 'NOW' (case independent)
130      * @return  corresponding <code>Time</code> value
131      * @exception java.lang.IllegalArgumentException if the given argument
132      * does not have the format <code>hh:mm:ss</code>
133      */
134     public static Time timeValue(String s) {
135 
136         if (s == null) {
137             throw new java.lang.IllegalArgumentException("null string");
138         }
139 
140         if (s.toUpperCase().equals("NOW")
141                 || s.toUpperCase().equals("CURRENT_TIME")) {
142             return new Time(System.currentTimeMillis());
143         }
144 
145         return Time.valueOf(s);
146     }
147 
148     private static final String sdftPattern  = "HH:mm:ss";
149     private static final String sdfdPattern  = "yyyy-MM-dd";
150     private static final String sdftsPattern = "yyyy-MM-dd HH:mm:ss.";
151 
152     public static java.sql.Date getDate(String dateString,
153                                         Calendar cal) throws SQLException {
154 
155         java.text.SimpleDateFormat sdfd = new SimpleDateFormat(sdfdPattern);
156 
157         sdfd.setCalendar(cal);
158 
159         try {
160             java.util.Date d = sdfd.parse(dateString);
161 
162             return new java.sql.Date(d.getTime());
163         } catch (java.text.ParseException e) {
164             throw invalidValue();
165         }
166     }
167 
168     public static Time getTime(String timeString,
169                                Calendar cal) throws SQLException {
170 
171         java.text.SimpleDateFormat sdft = new SimpleDateFormat(sdftPattern);
172 
173         sdft.setCalendar(cal);
174 
175         try {
176             java.util.Date d = sdft.parse(timeString);
177 
178             return new java.sql.Time(d.getTime());
179         } catch (java.text.ParseException e) {
180             throw invalidValue();
181         }
182     }
183 
184     public static Timestamp getTimestamp(String dateString,
185                                          Calendar cal) throws SQLException {
186 
187         java.text.SimpleDateFormat sdfts = new SimpleDateFormat(sdftsPattern);
188 
189         sdfts.setCalendar(cal);
190 
191         try {
192             java.util.Date d = sdfts.parse(dateString.substring(0,
193                 sdftsPattern.length()));
194             String nanostring = dateString.substring(sdftsPattern.length(),
195                 dateString.length());
196             java.sql.Timestamp ts = new java.sql.Timestamp(d.getTime());
197 
198             ts.setNanos(Integer.parseInt(nanostring));
199 
200             return ts;
201         } catch (java.text.ParseException e) {
202             throw invalidValue();
203         }
204     }
205 
206     private static java.text.SimpleDateFormat sdfts;
207 
208     public static String getTimestampString(Timestamp x,
209             Calendar cal) throws SQLException {
210 
211         SimpleDateFormat sdfts = new SimpleDateFormat(sdftsPattern);
212 
213         sdfts.setCalendar(cal);
214 
215         return sdfts.format(new java.util.Date(x.getTime()
216                                                + x.getNanos() / 1000000));
217     }
218 
219     public static String getTimeString(Time x,
220                                        Calendar cal) throws SQLException {
221 
222         final SimpleDateFormat sdft = new SimpleDateFormat(sdftPattern);
223 
224         sdft.setCalendar(cal);
225 
226         return sdft.format(new java.util.Date(x.getTime()));
227     }
228 
229     public static String getDateString(Date x,
230                                        Calendar cal) throws SQLException {
231 
232         SimpleDateFormat sdfd = new SimpleDateFormat(sdfdPattern);
233 
234         sdfd.setCalendar(cal);
235 
236         return sdfd.format(new java.util.Date(x.getTime()));
237     }
238 
239     static private SQLException invalidValue() {
240         return org.hsqldb.Trace.error(org.hsqldb.Trace.UNEXPECTED_TOKEN);
241     }
242     /*
243     public static void main(String[] args) {
244         String tests[] = { "2000-1-1", "2000-1-1 12:13", "2000-1-1 12:13:14",
245                            "2000-1-1 12:13:14.15" };
246         for (int i = 0; i < tests.length; i++) {
247             String test = tests[i];
248             System.out.print("test " + test + " = ");
249             try {
250                 System.out.println(HsqlDateTime.timestampValue(test));
251             } catch (Exception e) {
252                 System.out.println(e);
253             }
254         }
255     }
256 */
257 }