KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > DateTimeUtils


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.sql.Date JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.sql.Time JavaDoc;
10 import java.sql.Timestamp JavaDoc;
11 import java.util.Calendar JavaDoc;
12 import java.util.GregorianCalendar JavaDoc;
13 import java.util.TimeZone JavaDoc;
14
15 import org.h2.message.Message;
16 import org.h2.value.Value;
17 import org.h2.value.ValueDate;
18 import org.h2.value.ValueTime;
19 import org.h2.value.ValueTimestamp;
20
21 public class DateTimeUtils {
22     
23     public static Timestamp JavaDoc convertTimestampToCalendar(Timestamp JavaDoc x, Calendar JavaDoc calendar) throws SQLException JavaDoc {
24         if(x != null) {
25             Timestamp JavaDoc y = new Timestamp JavaDoc(getLocalTime(x, calendar));
26             // fix the nano seconds
27
y.setNanos(x.getNanos());
28             x = y;
29         }
30         return x;
31     }
32     
33     public static Time JavaDoc cloneAndNormalizeTime(Time JavaDoc value) {
34         Calendar JavaDoc cal = Calendar.getInstance();
35         cal.setTime(value);
36         // TODO gcj: required so that the millis are calculated?
37
cal.get(Calendar.HOUR_OF_DAY);
38         cal.set(1970, 0, 1);
39         return new Time JavaDoc(cal.getTime().getTime());
40     }
41     
42     public static Date JavaDoc cloneAndNormalizeDate(Date JavaDoc value) {
43         Calendar JavaDoc cal = Calendar.getInstance();
44         cal.setTime(value);
45         // TODO gcj: required so that the millis are calculated?
46
cal.get(Calendar.YEAR);
47         cal.set(Calendar.MILLISECOND, 0);
48         cal.set(Calendar.SECOND, 0);
49         cal.set(Calendar.MINUTE, 0);
50         cal.set(Calendar.HOUR_OF_DAY, 0);
51         return new Date JavaDoc(cal.getTime().getTime());
52     }
53
54     public static Value convertDateToUniversal(Date JavaDoc x, Calendar JavaDoc source) throws SQLException JavaDoc {
55         return ValueDate.get(new Date JavaDoc(getUniversalTime(source, x)));
56     }
57
58     public static Value convertTimeToUniversal(Time JavaDoc x, Calendar JavaDoc source) throws SQLException JavaDoc {
59         return ValueTime.get(new Time JavaDoc(getUniversalTime(source, x)));
60     }
61
62     public static Value convertTimestampToUniversal(Timestamp JavaDoc x, Calendar JavaDoc source) throws SQLException JavaDoc {
63         Timestamp JavaDoc y = new Timestamp JavaDoc(getUniversalTime(source, x));
64         // fix the nano seconds
65
y.setNanos(x.getNanos());
66         return ValueTimestamp.getNoCopy(y);
67     }
68
69     private static long getUniversalTime(Calendar JavaDoc source, java.util.Date JavaDoc x) throws SQLException JavaDoc {
70         if(source == null) {
71             throw Message.getInvalidValueException("calendar", null);
72         }
73         source = (Calendar JavaDoc)source.clone();
74         Calendar JavaDoc universal=Calendar.getInstance();
75         source.setTime(x);
76         convertTime(source, universal);
77         return universal.getTime().getTime();
78     }
79
80     private static long getLocalTime(java.util.Date JavaDoc x, Calendar JavaDoc target) throws SQLException JavaDoc {
81         if(target == null) {
82             throw Message.getInvalidValueException("calendar", null);
83         }
84         target = (Calendar JavaDoc)target.clone();
85         Calendar JavaDoc local=Calendar.getInstance();
86         local.setTime(x);
87         convertTime(local, target);
88         return target.getTime().getTime();
89     }
90
91     private static void convertTime(Calendar JavaDoc from, Calendar JavaDoc to) {
92         to.set(Calendar.YEAR, from.get(Calendar.YEAR));
93         to.set(Calendar.MONTH, from.get(Calendar.MONTH));
94         to.set(Calendar.DAY_OF_MONTH, from.get(Calendar.DAY_OF_MONTH));
95         to.set(Calendar.HOUR_OF_DAY, from.get(Calendar.HOUR_OF_DAY));
96         to.set(Calendar.MINUTE, from.get(Calendar.MINUTE));
97         to.set(Calendar.SECOND, from.get(Calendar.SECOND));
98         to.set(Calendar.MILLISECOND, from.get(Calendar.MILLISECOND));
99     }
100
101     public static Date JavaDoc convertDateToCalendar(Date JavaDoc x, Calendar JavaDoc calendar) throws SQLException JavaDoc {
102         return x == null ? null : new Date JavaDoc(getLocalTime(x, calendar));
103     }
104
105     public static Time JavaDoc convertTimeToCalendar(Time JavaDoc x, Calendar JavaDoc calendar) throws SQLException JavaDoc {
106         return x == null ? null : new Time JavaDoc(getLocalTime(x, calendar));
107     }
108     
109     public static java.util.Date JavaDoc parseDateTime(String JavaDoc s, int type, int errorCode) throws SQLException JavaDoc {
110         if (s == null) {
111             return null;
112         }
113         try {
114             int timeStart = 0;
115             TimeZone JavaDoc tz = null;
116             if(type == Value.TIME) {
117                 timeStart = 0;
118             } else {
119                 timeStart = s.indexOf(' ') + 1;
120                 if(timeStart <= 0) {
121                     // ISO 8601 compatibility
122
timeStart = s.indexOf('T') + 1;
123                 }
124             }
125             
126             int year = 1970, month = 1, day = 1;
127             if (type != Value.TIME) {
128                 // support +year
129
if(s.startsWith("+")) {
130                     s = s.substring(1);
131                 }
132                 // start at position 1 to support -year
133
int s1 = s.indexOf('-', 1);
134                 int s2 = s.indexOf('-', s1 + 1);
135                 if(s1 <= 0 || s2 <= s1) {
136                     throw Message.getSQLException(errorCode, s);
137                 }
138                 year = Integer.parseInt(s.substring(0, s1));
139                 month = Integer.parseInt(s.substring(s1 + 1, s2));
140                 int end = timeStart == 0 ? s.length() : timeStart - 1;
141                 day = Integer.parseInt(s.substring(s2 + 1, end));
142             }
143             int hour = 0, minute = 0, second = 0, nano = 0;
144             if (type != Value.DATE) {
145                 int s1 = s.indexOf(':', timeStart);
146                 int s2 = s.indexOf(':', s1 + 1);
147                 int s3 = s.indexOf('.', s2 + 1);
148                 if(s1 <= 0 || s2 <= s1) {
149                     throw Message.getSQLException(errorCode, s);
150                 }
151                 
152                 if(s.endsWith("Z")) {
153                     s = s.substring(0, s.length()-1);
154                 } else {
155                     int tzstart = s.indexOf('+', s2 + 1);
156                     if(tzstart < 0) {
157                         tzstart = s.indexOf('-', s2 + 1);
158                     }
159                     if(tzstart >= 0) {
160                         String JavaDoc tzName = "GMT" + s.substring(tzstart);
161                         tz = TimeZone.getTimeZone(tzName);
162                         if(!tz.getID().equals(tzName)) {
163                             throw Message.getSQLException(errorCode, s + " " + tz.getID() + "/" + tzName);
164                         }
165                         s = s.substring(0, tzstart);
166                     }
167                 }
168                 
169                 hour = Integer.parseInt(s.substring(timeStart, s1));
170                 minute = Integer.parseInt(s.substring(s1 + 1, s2));
171                 if (s3 < 0) {
172                     second = Integer.parseInt(s.substring(s2 + 1));
173                 } else {
174                     second = Integer.parseInt(s.substring(s2 + 1, s3));
175                     String JavaDoc n = (s + "000000000").substring(s3 + 1, s3 + 10);
176                     nano = Integer.parseInt(n);
177                 }
178             }
179             Calendar JavaDoc c;
180             if(tz == null) {
181                 c = Calendar.getInstance();
182             } else {
183                 c = Calendar.getInstance(tz);
184             }
185             c.setLenient(false);
186             long time;
187             if(year <= 0) {
188                 c.set(Calendar.ERA, GregorianCalendar.BC);
189                 c.set(Calendar.YEAR, 1 - year);
190             } else {
191                 c.set(Calendar.YEAR, year);
192             }
193             c.set(Calendar.MONTH, month - 1); // january is 0
194
c.set(Calendar.DAY_OF_MONTH, day);
195             c.set(Calendar.HOUR_OF_DAY, hour);
196             c.set(Calendar.MINUTE, minute);
197             c.set(Calendar.SECOND, second);
198             if(type != Value.TIMESTAMP) {
199                 c.set(Calendar.MILLISECOND, nano / 1000000);
200             }
201             time = c.getTime().getTime();
202             switch(type) {
203             case Value.DATE:
204                 return new java.sql.Date JavaDoc(time);
205             case Value.TIME:
206                 return new java.sql.Time JavaDoc(time);
207             case Value.TIMESTAMP: {
208                 Timestamp JavaDoc ts = new Timestamp JavaDoc(time);
209                 ts.setNanos(nano);
210                 return ts;
211             }
212             default:
213                 throw Message.getInternalError("type:"+type);
214             }
215         } catch(IllegalArgumentException JavaDoc e) {
216             throw Message.getSQLException(errorCode, s);
217         }
218     }
219
220 }
221
Popular Tags