提交 f38de87b authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Do not create a new calendar for isLeapYear() call

上级 9b90ba1f
...@@ -64,12 +64,12 @@ public class DateTimeUtils { ...@@ -64,12 +64,12 @@ public class DateTimeUtils {
* have that problem, and while it is still a small memory leak, it is not a * have that problem, and while it is still a small memory leak, it is not a
* class loader memory leak. * class loader memory leak.
*/ */
private static final ThreadLocal<Calendar> CACHED_CALENDAR = new ThreadLocal<>(); private static final ThreadLocal<GregorianCalendar> CACHED_CALENDAR = new ThreadLocal<>();
/** /**
* A cached instance of Calendar used when a timezone is specified. * A cached instance of Calendar used when a timezone is specified.
*/ */
private static final ThreadLocal<Calendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE = private static final ThreadLocal<GregorianCalendar> CACHED_CALENDAR_NON_DEFAULT_TIMEZONE =
new ThreadLocal<>(); new ThreadLocal<>();
/** /**
...@@ -101,8 +101,8 @@ public class DateTimeUtils { ...@@ -101,8 +101,8 @@ public class DateTimeUtils {
* *
* @return a calendar instance. A cached instance is returned where possible * @return a calendar instance. A cached instance is returned where possible
*/ */
private static Calendar getCalendar() { private static GregorianCalendar getCalendar() {
Calendar c = CACHED_CALENDAR.get(); GregorianCalendar c = CACHED_CALENDAR.get();
if (c == null) { if (c == null) {
c = DateTimeUtils.createGregorianCalendar(); c = DateTimeUtils.createGregorianCalendar();
CACHED_CALENDAR.set(c); CACHED_CALENDAR.set(c);
...@@ -117,8 +117,8 @@ public class DateTimeUtils { ...@@ -117,8 +117,8 @@ public class DateTimeUtils {
* @param tz timezone for the calendar, is never null * @param tz timezone for the calendar, is never null
* @return a calendar instance. A cached instance is returned where possible * @return a calendar instance. A cached instance is returned where possible
*/ */
private static Calendar getCalendar(TimeZone tz) { private static GregorianCalendar getCalendar(TimeZone tz) {
Calendar c = CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.get(); GregorianCalendar c = CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.get();
if (c == null || !c.getTimeZone().equals(tz)) { if (c == null || !c.getTimeZone().equals(tz)) {
c = DateTimeUtils.createGregorianCalendar(tz); c = DateTimeUtils.createGregorianCalendar(tz);
CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.set(c); CACHED_CALENDAR_NON_DEFAULT_TIMEZONE.set(c);
...@@ -136,7 +136,7 @@ public class DateTimeUtils { ...@@ -136,7 +136,7 @@ public class DateTimeUtils {
* *
* @return a new calendar instance. * @return a new calendar instance.
*/ */
public static Calendar createGregorianCalendar() { public static GregorianCalendar createGregorianCalendar() {
return new GregorianCalendar(); return new GregorianCalendar();
} }
...@@ -150,7 +150,7 @@ public class DateTimeUtils { ...@@ -150,7 +150,7 @@ public class DateTimeUtils {
* @param tz timezone for the calendar, is never null * @param tz timezone for the calendar, is never null
* @return a new calendar instance. * @return a new calendar instance.
*/ */
public static Calendar createGregorianCalendar(TimeZone tz) { public static GregorianCalendar createGregorianCalendar(TimeZone tz) {
return new GregorianCalendar(tz); return new GregorianCalendar(tz);
} }
...@@ -519,7 +519,7 @@ public class DateTimeUtils { ...@@ -519,7 +519,7 @@ public class DateTimeUtils {
*/ */
public static long getMillis(TimeZone tz, int year, int month, int day, public static long getMillis(TimeZone tz, int year, int month, int day,
int hour, int minute, int second, int millis) { int hour, int minute, int second, int millis) {
Calendar c; GregorianCalendar c;
if (tz == null) { if (tz == null) {
c = getCalendar(); c = getCalendar();
} else { } else {
...@@ -539,7 +539,7 @@ public class DateTimeUtils { ...@@ -539,7 +539,7 @@ public class DateTimeUtils {
} else if (message.indexOf("DAY_OF_MONTH") > 0) { } else if (message.indexOf("DAY_OF_MONTH") > 0) {
int maxDay; int maxDay;
if (month == 2) { if (month == 2) {
maxDay = new GregorianCalendar().isLeapYear(year) ? 29 : 28; maxDay = c.isLeapYear(year) ? 29 : 28;
} else { } else {
maxDay = 30 + ((month + (month > 7 ? 1 : 0)) & 1); maxDay = 30 + ((month + (month > 7 ? 1 : 0)) & 1);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论