mirror of https://github.com/apache/lucene.git
Don't re-create SimpleDateFormat objects, use static ones instead. Gives about a 2x performance increase in a micro benchmark.
git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@424449 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
895f763d21
commit
f72c1e579f
|
@ -40,6 +40,26 @@ public class DateTools {
|
||||||
|
|
||||||
private final static TimeZone GMT = TimeZone.getTimeZone("GMT");
|
private final static TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||||
|
|
||||||
|
private static final SimpleDateFormat YEAR_FORMAT = new SimpleDateFormat("yyyy");
|
||||||
|
private static final SimpleDateFormat MONTH_FORMAT = new SimpleDateFormat("yyyyMM");
|
||||||
|
private static final SimpleDateFormat DAY_FORMAT = new SimpleDateFormat("yyyyMMdd");
|
||||||
|
private static final SimpleDateFormat HOUR_FORMAT = new SimpleDateFormat("yyyyMMddHH");
|
||||||
|
private static final SimpleDateFormat MINUTE_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
|
||||||
|
private static final SimpleDateFormat SECOND_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||||
|
private static final SimpleDateFormat MILLISECOND_FORMAT = new SimpleDateFormat("yyyyMMddHHmmssSSS");
|
||||||
|
static {
|
||||||
|
// times need to be normalized so the value doesn't depend on the
|
||||||
|
// location the index is created/used:
|
||||||
|
YEAR_FORMAT.setTimeZone(GMT);
|
||||||
|
MONTH_FORMAT.setTimeZone(GMT);
|
||||||
|
DAY_FORMAT.setTimeZone(GMT);
|
||||||
|
HOUR_FORMAT.setTimeZone(GMT);
|
||||||
|
MINUTE_FORMAT.setTimeZone(GMT);
|
||||||
|
SECOND_FORMAT.setTimeZone(GMT);
|
||||||
|
MILLISECOND_FORMAT.setTimeZone(GMT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// cannot create, the class has static methods only
|
||||||
private DateTools() {}
|
private DateTools() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,28 +92,39 @@ public class DateTools {
|
||||||
|
|
||||||
cal.setTime(new Date(round(time, resolution)));
|
cal.setTime(new Date(round(time, resolution)));
|
||||||
|
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat();
|
String result;
|
||||||
sdf.setTimeZone(GMT);
|
|
||||||
String pattern = null;
|
|
||||||
if (resolution == Resolution.YEAR) {
|
if (resolution == Resolution.YEAR) {
|
||||||
pattern = "yyyy";
|
synchronized (YEAR_FORMAT) {
|
||||||
|
result = YEAR_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else if (resolution == Resolution.MONTH) {
|
} else if (resolution == Resolution.MONTH) {
|
||||||
pattern = "yyyyMM";
|
synchronized (MONTH_FORMAT) {
|
||||||
|
result = MONTH_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else if (resolution == Resolution.DAY) {
|
} else if (resolution == Resolution.DAY) {
|
||||||
pattern = "yyyyMMdd";
|
synchronized (DAY_FORMAT) {
|
||||||
|
result = DAY_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else if (resolution == Resolution.HOUR) {
|
} else if (resolution == Resolution.HOUR) {
|
||||||
pattern = "yyyyMMddHH";
|
synchronized (HOUR_FORMAT) {
|
||||||
|
result = HOUR_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else if (resolution == Resolution.MINUTE) {
|
} else if (resolution == Resolution.MINUTE) {
|
||||||
pattern = "yyyyMMddHHmm";
|
synchronized (MINUTE_FORMAT) {
|
||||||
|
result = MINUTE_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else if (resolution == Resolution.SECOND) {
|
} else if (resolution == Resolution.SECOND) {
|
||||||
pattern = "yyyyMMddHHmmss";
|
synchronized (SECOND_FORMAT) {
|
||||||
|
result = SECOND_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else if (resolution == Resolution.MILLISECOND) {
|
} else if (resolution == Resolution.MILLISECOND) {
|
||||||
pattern = "yyyyMMddHHmmssSSS";
|
synchronized (MILLISECOND_FORMAT) {
|
||||||
|
result = MILLISECOND_FORMAT.format(cal.getTime());
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("unknown resolution " + resolution);
|
throw new IllegalArgumentException("unknown resolution " + resolution);
|
||||||
}
|
}
|
||||||
sdf.applyPattern(pattern);
|
return result;
|
||||||
return sdf.format(cal.getTime());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -121,26 +152,38 @@ public class DateTools {
|
||||||
* expected format
|
* expected format
|
||||||
*/
|
*/
|
||||||
public static Date stringToDate(String dateString) throws ParseException {
|
public static Date stringToDate(String dateString) throws ParseException {
|
||||||
String pattern = null;
|
Date date;
|
||||||
if (dateString.length() == 4 )
|
if (dateString.length() == 4) {
|
||||||
pattern = "yyyy";
|
synchronized (YEAR_FORMAT) {
|
||||||
else if (dateString.length() == 6 )
|
date = YEAR_FORMAT.parse(dateString);
|
||||||
pattern = "yyyyMM";
|
}
|
||||||
else if (dateString.length() == 8 )
|
} else if (dateString.length() == 6) {
|
||||||
pattern = "yyyyMMdd";
|
synchronized (MONTH_FORMAT) {
|
||||||
else if (dateString.length() == 10 )
|
date = MONTH_FORMAT.parse(dateString);
|
||||||
pattern = "yyyyMMddHH";
|
}
|
||||||
else if (dateString.length() == 12 )
|
} else if (dateString.length() == 8) {
|
||||||
pattern = "yyyyMMddHHmm";
|
synchronized (DAY_FORMAT) {
|
||||||
else if (dateString.length() == 14 )
|
date = DAY_FORMAT.parse(dateString);
|
||||||
pattern = "yyyyMMddHHmmss";
|
}
|
||||||
else if (dateString.length() == 17 )
|
} else if (dateString.length() == 10) {
|
||||||
pattern = "yyyyMMddHHmmssSSS";
|
synchronized (HOUR_FORMAT) {
|
||||||
else
|
date = HOUR_FORMAT.parse(dateString);
|
||||||
|
}
|
||||||
|
} else if (dateString.length() == 12) {
|
||||||
|
synchronized (MINUTE_FORMAT) {
|
||||||
|
date = MINUTE_FORMAT.parse(dateString);
|
||||||
|
}
|
||||||
|
} else if (dateString.length() == 14) {
|
||||||
|
synchronized (SECOND_FORMAT) {
|
||||||
|
date = SECOND_FORMAT.parse(dateString);
|
||||||
|
}
|
||||||
|
} else if (dateString.length() == 17) {
|
||||||
|
synchronized (MILLISECOND_FORMAT) {
|
||||||
|
date = MILLISECOND_FORMAT.parse(dateString);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
throw new ParseException("Input is not valid date string: " + dateString, 0);
|
throw new ParseException("Input is not valid date string: " + dateString, 0);
|
||||||
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
|
}
|
||||||
sdf.setTimeZone(GMT);
|
|
||||||
Date date = sdf.parse(dateString);
|
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue