|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DateTools | Line # 39 | 92 | 29 | 96.5% |
0.96478873
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| DateTools.Resolution | Line # 257 | 2 | 3 | 40% |
0.4
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (8) | |||
| Result | |||
|
0.8503401
|
org.apache.lucene.document.TestDateTools.testDateAndTimetoString
org.apache.lucene.document.TestDateTools.testDateAndTimetoString
|
1 PASS | |
|
0.3537415
|
org.apache.lucene.document.TestDateTools.testDateToolsUTC
org.apache.lucene.document.TestDateTools.testDateToolsUTC
|
1 PASS | |
|
0.33333334
|
org.apache.lucene.search.TestDateFilter.testBefore
org.apache.lucene.search.TestDateFilter.testBefore
|
1 PASS | |
|
0.3197279
|
org.apache.lucene.document.TestDateTools.testRound
org.apache.lucene.document.TestDateTools.testRound
|
1 PASS | |
|
0.292517
|
org.apache.lucene.index.TestDoc.testIndexAndMerge
org.apache.lucene.index.TestDoc.testIndexAndMerge
|
1 PASS | |
|
0.26530612
|
org.apache.lucene.search.TestDateFilter.testAfter
org.apache.lucene.search.TestDateFilter.testAfter
|
1 PASS | |
|
0.2585034
|
org.apache.lucene.document.TestDateTools.testStringToDate
org.apache.lucene.document.TestDateTools.testStringToDate
|
1 PASS | |
|
0.11564626
|
org.apache.lucene.document.TestDateTools.testStringtoTime
org.apache.lucene.document.TestDateTools.testStringtoTime
|
1 PASS | |
| 1 | package org.apache.lucene.document; | |
| 2 | ||
| 3 | /** | |
| 4 | * Copyright 2004 The Apache Software Foundation | |
| 5 | * | |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 7 | * you may not use this file except in compliance with the License. | |
| 8 | * You may obtain a copy of the License at | |
| 9 | * | |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 11 | * | |
| 12 | * Unless required by applicable law or agreed to in writing, software | |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 15 | * See the License for the specific language governing permissions and | |
| 16 | * limitations under the License. | |
| 17 | */ | |
| 18 | ||
| 19 | import java.text.ParseException; | |
| 20 | import java.text.SimpleDateFormat; | |
| 21 | import java.util.Calendar; | |
| 22 | import java.util.Date; | |
| 23 | import java.util.TimeZone; | |
| 24 | ||
| 25 | /** | |
| 26 | * Provides support for converting dates to strings and vice-versa. | |
| 27 | * The strings are structured so that lexicographic sorting orders | |
| 28 | * them by date, which makes them suitable for use as field values | |
| 29 | * and search terms. | |
| 30 | * | |
| 31 | * <P>This class also helps you to limit the resolution of your dates. Do not | |
| 32 | * save dates with a finer resolution than you really need, as then | |
| 33 | * RangeQuery and PrefixQuery will require more memory and become slower. | |
| 34 | * | |
| 35 | * <P>Compared to {@link DateField} the strings generated by the methods | |
| 36 | * in this class take slightly more space, unless your selected resolution | |
| 37 | * is set to <code>Resolution.DAY</code> or lower. | |
| 38 | */ | |
| 39 | public class DateTools { | |
| 40 | ||
| 41 | private final static TimeZone GMT = TimeZone.getTimeZone("GMT"); | |
| 42 | ||
| 43 | private static final SimpleDateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy"); | |
| 44 | private static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("yyyyMM"); | |
| 45 | private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("yyyyMMdd"); | |
| 46 | private static final SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat("yyyyMMddHH"); | |
| 47 | private static final SimpleDateFormat MINUTE_FORMAT = new SimpleDateFormat("yyyyMMddHHmm"); | |
| 48 | private static final SimpleDateFormat SECOND_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss"); | |
| 49 | private static final SimpleDateFormat MILLISECOND_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS"); | |
| 50 | 4 |
static { |
| 51 | // times need to be normalized so the value doesn't depend on the | |
| 52 | // location the index is created/used: | |
| 53 | 4 | YEAR_FORMAT.setTimeZone(GMT); |
| 54 | 4 | MONTH_FORMAT.setTimeZone(GMT); |
| 55 | 4 | DAY_FORMAT.setTimeZone(GMT); |
| 56 | 4 | HOUR_FORMAT.setTimeZone(GMT); |
| 57 | 4 | MINUTE_FORMAT.setTimeZone(GMT); |
| 58 | 4 | SECOND_FORMAT.setTimeZone(GMT); |
| 59 | 4 | MILLISECOND_FORMAT.setTimeZone(GMT); |
| 60 | } | |
| 61 | ||
| 62 | // cannot create, the class has static methods only | |
| 63 | 0 |
private DateTools() {} |
| 64 | ||
| 65 | /** | |
| 66 | * Converts a Date to a string suitable for indexing. | |
| 67 | * | |
| 68 | * @param date the date to be converted | |
| 69 | * @param resolution the desired resolution, see | |
| 70 | * {@link #round(Date, DateTools.Resolution)} | |
| 71 | * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, | |
| 72 | * depeding on <code>resolution</code>; using UTC as timezone | |
| 73 | */ | |
| 74 | 11 |
public static String dateToString(Date date, Resolution resolution) { |
| 75 | 11 | return timeToString(date.getTime(), resolution); |
| 76 | } | |
| 77 | ||
| 78 | /** | |
| 79 | * Converts a millisecond time to a string suitable for indexing. | |
| 80 | * | |
| 81 | * @param time the date expressed as milliseconds since January 1, 1970, 00:00:00 GMT | |
| 82 | * @param resolution the desired resolution, see | |
| 83 | * {@link #round(long, DateTools.Resolution)} | |
| 84 | * @return a string in format <code>yyyyMMddHHmmssSSS</code> or shorter, | |
| 85 | * depeding on <code>resolution</code>; using UTC as timezone | |
| 86 | */ | |
| 87 | 4827 |
public static String timeToString(long time, Resolution resolution) { |
| 88 | 4827 | Calendar cal = Calendar.getInstance(GMT); |
| 89 | ||
| 90 | //protected in JDK's prior to 1.4 | |
| 91 | //cal.setTimeInMillis(round(time, resolution)); | |
| 92 | ||
| 93 | 4827 | cal.setTime(new Date(round(time, resolution))); |
| 94 | ||
| 95 | 4827 | String result; |
| 96 | 4827 | if (resolution == Resolution.YEAR) { |
| 97 | 1 | synchronized (YEAR_FORMAT) { |
| 98 | 1 | result = YEAR_FORMAT.format(cal.getTime()); |
| 99 | } | |
| 100 | 4826 | } else if (resolution == Resolution.MONTH) { |
| 101 | 1 | synchronized (MONTH_FORMAT) { |
| 102 | 1 | result = MONTH_FORMAT.format(cal.getTime()); |
| 103 | } | |
| 104 | 4825 | } else if (resolution == Resolution.DAY) { |
| 105 | 4801 | synchronized (DAY_FORMAT) { |
| 106 | 4801 | result = DAY_FORMAT.format(cal.getTime()); |
| 107 | } | |
| 108 | 24 | } else if (resolution == Resolution.HOUR) { |
| 109 | 2 | synchronized (HOUR_FORMAT) { |
| 110 | 2 | result = HOUR_FORMAT.format(cal.getTime()); |
| 111 | } | |
| 112 | 22 | } else if (resolution == Resolution.MINUTE) { |
| 113 | 7 | synchronized (MINUTE_FORMAT) { |
| 114 | 7 | result = MINUTE_FORMAT.format(cal.getTime()); |
| 115 | } | |
| 116 | 15 | } else if (resolution == Resolution.SECOND) { |
| 117 | 1 | synchronized (SECOND_FORMAT) { |
| 118 | 1 | result = SECOND_FORMAT.format(cal.getTime()); |
| 119 | } | |
| 120 | 14 | } else if (resolution == Resolution.MILLISECOND) { |
| 121 | 14 | synchronized (MILLISECOND_FORMAT) { |
| 122 | 14 | result = MILLISECOND_FORMAT.format(cal.getTime()); |
| 123 | } | |
| 124 | } else { | |
| 125 | 0 | throw new IllegalArgumentException("unknown resolution " + resolution); |
| 126 | } | |
| 127 | 4827 | return result; |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Converts a string produced by <code>timeToString</code> or | |
| 132 | * <code>dateToString</code> back to a time, represented as the | |
| 133 | * number of milliseconds since January 1, 1970, 00:00:00 GMT. | |
| 134 | * | |
| 135 | * @param dateString the date string to be converted | |
| 136 | * @return the number of milliseconds since January 1, 1970, 00:00:00 GMT | |
| 137 | * @throws ParseException if <code>dateString</code> is not in the | |
| 138 | * expected format | |
| 139 | */ | |
| 140 | 4 |
public static long stringToTime(String dateString) throws ParseException { |
| 141 | 4 | return stringToDate(dateString).getTime(); |
| 142 | } | |
| 143 | ||
| 144 | /** | |
| 145 | * Converts a string produced by <code>timeToString</code> or | |
| 146 | * <code>dateToString</code> back to a time, represented as a | |
| 147 | * Date object. | |
| 148 | * | |
| 149 | * @param dateString the date string to be converted | |
| 150 | * @return the parsed time as a Date object | |
| 151 | * @throws ParseException if <code>dateString</code> is not in the | |
| 152 | * expected format | |
| 153 | */ | |
| 154 | 20 |
public static Date stringToDate(String dateString) throws ParseException { |
| 155 | 20 | Date date; |
| 156 | 20 | if (dateString.length() == 4) { |
| 157 | 3 | synchronized (YEAR_FORMAT) { |
| 158 | 3 | date = YEAR_FORMAT.parse(dateString); |