提交 d5b47315 authored 作者: Thomas Mueller's avatar Thomas Mueller

Merge pull request #163 from stevemcleod/datetimeutils_tweak

In DateTimeUtils.getTimeTry(), Calendar instances used to evaluate Timestamps were only being cached if no timezone was specified. I've added code to reuse Calendar instances if the timezone specified matches the previous use.
...@@ -62,6 +62,12 @@ public class DateTimeUtils { ...@@ -62,6 +62,12 @@ public class DateTimeUtils {
private static final ThreadLocal<Calendar> CACHED_CALENDAR = private static final ThreadLocal<Calendar> CACHED_CALENDAR =
new ThreadLocal<Calendar>(); new ThreadLocal<Calendar>();
/**
* A cached instance of Calendar used when a timezone is specified.
*/
private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE =
new ThreadLocal<Calendar>();
private DateTimeUtils() { private DateTimeUtils() {
// utility class // utility class
} }
...@@ -82,6 +88,19 @@ public class DateTimeUtils { ...@@ -82,6 +88,19 @@ public class DateTimeUtils {
return c; return c;
} }
/**
* @param tz timezone for the calendar, is never null
* @return a calendar instance for the specified timezone. A cached instance is returned where possible
*/
private static Calendar getCalendar(TimeZone tz) {
Calendar c = CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.get();
if (c == null || !c.getTimeZone().equals(tz)) {
c = Calendar.getInstance(tz);
CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.set(c);
}
return c;
}
/** /**
* Convert the date to the specified time zone. * Convert the date to the specified time zone.
* *
...@@ -394,7 +413,7 @@ public class DateTimeUtils { ...@@ -394,7 +413,7 @@ public class DateTimeUtils {
if (tz == null) { if (tz == null) {
c = getCalendar(); c = getCalendar();
} else { } else {
c = Calendar.getInstance(tz); c = getCalendar(tz);
} }
c.clear(); c.clear();
c.setLenient(lenient); c.setLenient(lenient);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论