001: package org.apache.lucene.document;
002:
003: import java.text.ParseException;
004: import java.text.SimpleDateFormat;
005: import java.util.Calendar;
006: import java.util.Date;
007: import java.util.TimeZone;
008:
009: import org.apache.lucene.util.LuceneTestCase;
010:
011: /**
012: * Licensed to the Apache Software Foundation (ASF) under one or more
013: * contributor license agreements. See the NOTICE file distributed with
014: * this work for additional information regarding copyright ownership.
015: * The ASF licenses this file to You under the Apache License, Version 2.0
016: * (the "License"); you may not use this file except in compliance with
017: * the License. You may obtain a copy of the License at
018: *
019: * http://www.apache.org/licenses/LICENSE-2.0
020: *
021: * Unless required by applicable law or agreed to in writing, software
022: * distributed under the License is distributed on an "AS IS" BASIS,
023: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
024: * See the License for the specific language governing permissions and
025: * limitations under the License.
026: */
027:
028: public class TestDateTools extends LuceneTestCase {
029:
030: public void testStringToDate() throws ParseException {
031:
032: Date d = null;
033: d = DateTools.stringToDate("2004");
034: assertEquals("2004-01-01 00:00:00:000", isoFormat(d));
035: d = DateTools.stringToDate("20040705");
036: assertEquals("2004-07-05 00:00:00:000", isoFormat(d));
037: d = DateTools.stringToDate("200407050910");
038: assertEquals("2004-07-05 09:10:00:000", isoFormat(d));
039: d = DateTools.stringToDate("20040705091055990");
040: assertEquals("2004-07-05 09:10:55:990", isoFormat(d));
041:
042: try {
043: d = DateTools.stringToDate("97"); // no date
044: fail();
045: } catch (ParseException e) { /* expected exception */
046: }
047: try {
048: d = DateTools.stringToDate("200401011235009999"); // no date
049: fail();
050: } catch (ParseException e) { /* expected exception */
051: }
052: try {
053: d = DateTools.stringToDate("aaaa"); // no date
054: fail();
055: } catch (ParseException e) { /* expected exception */
056: }
057:
058: }
059:
060: public void testStringtoTime() throws ParseException {
061: long time = DateTools.stringToTime("197001010000");
062: Calendar cal = Calendar.getInstance();
063: cal.set(1970, 0, 1, // year=1970, month=january, day=1
064: 0, 0, 0); // hour, minute, second
065: cal.set(Calendar.MILLISECOND, 0);
066: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
067: assertEquals(cal.getTime().getTime(), time);
068: cal.set(1980, 1, 2, // year=1980, month=february, day=2
069: 11, 5, 0); // hour, minute, second
070: cal.set(Calendar.MILLISECOND, 0);
071: time = DateTools.stringToTime("198002021105");
072: assertEquals(cal.getTime().getTime(), time);
073: }
074:
075: public void testDateAndTimetoString() throws ParseException {
076: Calendar cal = Calendar.getInstance();
077: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
078: cal.set(2004, 1, 3, // year=2004, month=february(!), day=3
079: 22, 8, 56); // hour, minute, second
080: cal.set(Calendar.MILLISECOND, 333);
081:
082: String dateString;
083: dateString = DateTools.dateToString(cal.getTime(),
084: DateTools.Resolution.YEAR);
085: assertEquals("2004", dateString);
086: assertEquals("2004-01-01 00:00:00:000", isoFormat(DateTools
087: .stringToDate(dateString)));
088:
089: dateString = DateTools.dateToString(cal.getTime(),
090: DateTools.Resolution.MONTH);
091: assertEquals("200402", dateString);
092: assertEquals("2004-02-01 00:00:00:000", isoFormat(DateTools
093: .stringToDate(dateString)));
094:
095: dateString = DateTools.dateToString(cal.getTime(),
096: DateTools.Resolution.DAY);
097: assertEquals("20040203", dateString);
098: assertEquals("2004-02-03 00:00:00:000", isoFormat(DateTools
099: .stringToDate(dateString)));
100:
101: dateString = DateTools.dateToString(cal.getTime(),
102: DateTools.Resolution.HOUR);
103: assertEquals("2004020322", dateString);
104: assertEquals("2004-02-03 22:00:00:000", isoFormat(DateTools
105: .stringToDate(dateString)));
106:
107: dateString = DateTools.dateToString(cal.getTime(),
108: DateTools.Resolution.MINUTE);
109: assertEquals("200402032208", dateString);
110: assertEquals("2004-02-03 22:08:00:000", isoFormat(DateTools
111: .stringToDate(dateString)));
112:
113: dateString = DateTools.dateToString(cal.getTime(),
114: DateTools.Resolution.SECOND);
115: assertEquals("20040203220856", dateString);
116: assertEquals("2004-02-03 22:08:56:000", isoFormat(DateTools
117: .stringToDate(dateString)));
118:
119: dateString = DateTools.dateToString(cal.getTime(),
120: DateTools.Resolution.MILLISECOND);
121: assertEquals("20040203220856333", dateString);
122: assertEquals("2004-02-03 22:08:56:333", isoFormat(DateTools
123: .stringToDate(dateString)));
124:
125: // date before 1970:
126: cal.set(1961, 2, 5, // year=1961, month=march(!), day=5
127: 23, 9, 51); // hour, minute, second
128: cal.set(Calendar.MILLISECOND, 444);
129: dateString = DateTools.dateToString(cal.getTime(),
130: DateTools.Resolution.MILLISECOND);
131: assertEquals("19610305230951444", dateString);
132: assertEquals("1961-03-05 23:09:51:444", isoFormat(DateTools
133: .stringToDate(dateString)));
134:
135: dateString = DateTools.dateToString(cal.getTime(),
136: DateTools.Resolution.HOUR);
137: assertEquals("1961030523", dateString);
138: assertEquals("1961-03-05 23:00:00:000", isoFormat(DateTools
139: .stringToDate(dateString)));
140:
141: // timeToString:
142: cal.set(1970, 0, 1, // year=1970, month=january, day=1
143: 0, 0, 0); // hour, minute, second
144: cal.set(Calendar.MILLISECOND, 0);
145: dateString = DateTools.timeToString(cal.getTime().getTime(),
146: DateTools.Resolution.MILLISECOND);
147: assertEquals("19700101000000000", dateString);
148:
149: cal.set(1970, 0, 1, // year=1970, month=january, day=1
150: 1, 2, 3); // hour, minute, second
151: cal.set(Calendar.MILLISECOND, 0);
152: dateString = DateTools.timeToString(cal.getTime().getTime(),
153: DateTools.Resolution.MILLISECOND);
154: assertEquals("19700101010203000", dateString);
155: }
156:
157: public void testRound() {
158: Calendar cal = Calendar.getInstance();
159: cal.setTimeZone(TimeZone.getTimeZone("GMT"));
160: cal.set(2004, 1, 3, // year=2004, month=february(!), day=3
161: 22, 8, 56); // hour, minute, second
162: cal.set(Calendar.MILLISECOND, 333);
163: Date date = cal.getTime();
164: assertEquals("2004-02-03 22:08:56:333", isoFormat(date));
165:
166: Date dateYear = DateTools
167: .round(date, DateTools.Resolution.YEAR);
168: assertEquals("2004-01-01 00:00:00:000", isoFormat(dateYear));
169:
170: Date dateMonth = DateTools.round(date,
171: DateTools.Resolution.MONTH);
172: assertEquals("2004-02-01 00:00:00:000", isoFormat(dateMonth));
173:
174: Date dateDay = DateTools.round(date, DateTools.Resolution.DAY);
175: assertEquals("2004-02-03 00:00:00:000", isoFormat(dateDay));
176:
177: Date dateHour = DateTools
178: .round(date, DateTools.Resolution.HOUR);
179: assertEquals("2004-02-03 22:00:00:000", isoFormat(dateHour));
180:
181: Date dateMinute = DateTools.round(date,
182: DateTools.Resolution.MINUTE);
183: assertEquals("2004-02-03 22:08:00:000", isoFormat(dateMinute));
184:
185: Date dateSecond = DateTools.round(date,
186: DateTools.Resolution.SECOND);
187: assertEquals("2004-02-03 22:08:56:000", isoFormat(dateSecond));
188:
189: Date dateMillisecond = DateTools.round(date,
190: DateTools.Resolution.MILLISECOND);
191: assertEquals("2004-02-03 22:08:56:333",
192: isoFormat(dateMillisecond));
193:
194: // long parameter:
195: long dateYearLong = DateTools.round(date.getTime(),
196: DateTools.Resolution.YEAR);
197: assertEquals("2004-01-01 00:00:00:000", isoFormat(new Date(
198: dateYearLong)));
199:
200: long dateMillisecondLong = DateTools.round(date.getTime(),
201: DateTools.Resolution.MILLISECOND);
202: assertEquals("2004-02-03 22:08:56:333", isoFormat(new Date(
203: dateMillisecondLong)));
204: }
205:
206: private String isoFormat(Date date) {
207: SimpleDateFormat sdf = new SimpleDateFormat(
208: "yyyy-MM-dd HH:mm:ss:SSS");
209: sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
210: return sdf.format(date);
211: }
212:
213: public void testDateToolsUTC() throws Exception {
214: // Sun, 30 Oct 2005 00:00:00 +0000 -- the last second of 2005's DST in Europe/London
215: long time = 1130630400;
216: try {
217: TimeZone.setDefault(TimeZone
218: .getTimeZone(/* "GMT" */"Europe/London"));
219: String d1 = DateTools.dateToString(new Date(time * 1000),
220: DateTools.Resolution.MINUTE);
221: String d2 = DateTools.dateToString(new Date(
222: (time + 3600) * 1000), DateTools.Resolution.MINUTE);
223: assertFalse("different times", d1.equals(d2));
224: assertEquals("midnight", DateTools.stringToTime(d1),
225: time * 1000);
226: assertEquals("later", DateTools.stringToTime(d2),
227: (time + 3600) * 1000);
228: } finally {
229: TimeZone.setDefault(null);
230: }
231: }
232:
233: }
|