mirror of https://github.com/apache/lucene.git
test cases for the DateTools class
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150484 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
c7fbb991c3
commit
bdcaaa7e62
|
@ -0,0 +1,160 @@
|
||||||
|
package org.apache.lucene.document;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright 2004 The Apache Software Foundation
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class TestDateTools extends TestCase {
|
||||||
|
|
||||||
|
public void testStringToDate() throws ParseException {
|
||||||
|
|
||||||
|
Date d = null;
|
||||||
|
d = DateTools.stringToDate("2004");
|
||||||
|
assertEquals("2004-01-01 00:00:00:000", isoFormat(d));
|
||||||
|
d = DateTools.stringToDate("20040705");
|
||||||
|
assertEquals("2004-07-05 00:00:00:000", isoFormat(d));
|
||||||
|
d = DateTools.stringToDate("200407050910");
|
||||||
|
assertEquals("2004-07-05 09:10:00:000", isoFormat(d));
|
||||||
|
d = DateTools.stringToDate("20040705091055990");
|
||||||
|
assertEquals("2004-07-05 09:10:55:990", isoFormat(d));
|
||||||
|
|
||||||
|
try {
|
||||||
|
d = DateTools.stringToDate("97"); // no date
|
||||||
|
fail();
|
||||||
|
} catch(ParseException e) { /* expected excpetion */ }
|
||||||
|
try {
|
||||||
|
d = DateTools.stringToDate("200401011235009999"); // no date
|
||||||
|
fail();
|
||||||
|
} catch(ParseException e) { /* expected excpetion */ }
|
||||||
|
try {
|
||||||
|
d = DateTools.stringToDate("aaaa"); // no date
|
||||||
|
fail();
|
||||||
|
} catch(ParseException e) { /* expected excpetion */ }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testStringtotime() throws ParseException {
|
||||||
|
long time = DateTools.stringToTime("197001010100");
|
||||||
|
assertEquals(0, time);
|
||||||
|
time = DateTools.stringToTime("197001010102");
|
||||||
|
assertEquals(120000, time);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testDateAndTimetoString() throws ParseException {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.set(2004, 1, 3, // year=2004, month=february(!), day=3
|
||||||
|
22, 8, 56); // hour, minute, second
|
||||||
|
cal.set(Calendar.MILLISECOND, 333);
|
||||||
|
|
||||||
|
String dateString;
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.YEAR);
|
||||||
|
assertEquals("2004", dateString);
|
||||||
|
assertEquals("2004-01-01 00:00:00:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.MONTH);
|
||||||
|
assertEquals("200402", dateString);
|
||||||
|
assertEquals("2004-02-01 00:00:00:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.DAY);
|
||||||
|
assertEquals("20040203", dateString);
|
||||||
|
assertEquals("2004-02-03 00:00:00:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.HOUR);
|
||||||
|
assertEquals("2004020322", dateString);
|
||||||
|
assertEquals("2004-02-03 22:00:00:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.MINUTE);
|
||||||
|
assertEquals("200402032208", dateString);
|
||||||
|
assertEquals("2004-02-03 22:08:00:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.SECOND);
|
||||||
|
assertEquals("20040203220856", dateString);
|
||||||
|
assertEquals("2004-02-03 22:08:56:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.MILLISECOND);
|
||||||
|
assertEquals("20040203220856333", dateString);
|
||||||
|
assertEquals("2004-02-03 22:08:56:333", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
// date before 1970:
|
||||||
|
cal.set(1961, 2, 5, // year=1961, month=march(!), day=5
|
||||||
|
23, 9, 51); // hour, minute, second
|
||||||
|
cal.set(Calendar.MILLISECOND, 444);
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.MILLISECOND);
|
||||||
|
assertEquals("19610305230951444", dateString);
|
||||||
|
assertEquals("1961-03-05 23:09:51:444", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
dateString = DateTools.dateToString(cal.getTime(), DateTools.Resolution.HOUR);
|
||||||
|
assertEquals("1961030523", dateString);
|
||||||
|
assertEquals("1961-03-05 23:00:00:000", isoFormat(DateTools.stringToDate(dateString)));
|
||||||
|
|
||||||
|
// timeToString:
|
||||||
|
dateString = DateTools.timeToString(0, DateTools.Resolution.MILLISECOND);
|
||||||
|
assertEquals("19700101010000000", dateString);
|
||||||
|
|
||||||
|
dateString = DateTools.timeToString(123000, DateTools.Resolution.MILLISECOND);
|
||||||
|
assertEquals("19700101010203000", dateString);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRound() {
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.set(2004, 1, 3, // year=2004, month=february(!), day=3
|
||||||
|
22, 8, 56); // hour, minute, second
|
||||||
|
cal.set(Calendar.MILLISECOND, 333);
|
||||||
|
Date date = cal.getTime();
|
||||||
|
assertEquals("2004-02-03 22:08:56:333", isoFormat(date));
|
||||||
|
|
||||||
|
Date dateYear = DateTools.round(date, DateTools.Resolution.YEAR);
|
||||||
|
assertEquals("2004-01-01 00:00:00:000", isoFormat(dateYear));
|
||||||
|
|
||||||
|
Date dateMonth = DateTools.round(date, DateTools.Resolution.MONTH);
|
||||||
|
assertEquals("2004-02-01 00:00:00:000", isoFormat(dateMonth));
|
||||||
|
|
||||||
|
Date dateDay = DateTools.round(date, DateTools.Resolution.DAY);
|
||||||
|
assertEquals("2004-02-03 00:00:00:000", isoFormat(dateDay));
|
||||||
|
|
||||||
|
Date dateHour = DateTools.round(date, DateTools.Resolution.HOUR);
|
||||||
|
assertEquals("2004-02-03 22:00:00:000", isoFormat(dateHour));
|
||||||
|
|
||||||
|
Date dateMinute = DateTools.round(date, DateTools.Resolution.MINUTE);
|
||||||
|
assertEquals("2004-02-03 22:08:00:000", isoFormat(dateMinute));
|
||||||
|
|
||||||
|
Date dateSecond = DateTools.round(date, DateTools.Resolution.SECOND);
|
||||||
|
assertEquals("2004-02-03 22:08:56:000", isoFormat(dateSecond));
|
||||||
|
|
||||||
|
Date dateMillisecond = DateTools.round(date, DateTools.Resolution.MILLISECOND);
|
||||||
|
assertEquals("2004-02-03 22:08:56:333", isoFormat(dateMillisecond));
|
||||||
|
|
||||||
|
// long parameter:
|
||||||
|
long dateYearLong = DateTools.round(date.getTime(), DateTools.Resolution.YEAR);
|
||||||
|
assertEquals("2004-01-01 00:00:00:000", isoFormat(new Date(dateYearLong)));
|
||||||
|
|
||||||
|
long dateMillisecondLong = DateTools.round(date.getTime(), DateTools.Resolution.MILLISECOND);
|
||||||
|
assertEquals("2004-02-03 22:08:56:333", isoFormat(new Date(dateMillisecondLong)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String isoFormat(Date date) {
|
||||||
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
|
||||||
|
return sdf.format(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue