diff --git a/core/src/main/java/lucee/aprint.java b/core/src/main/java/lucee/aprint.java index dcc5fd9054..337e64fca0 100755 --- a/core/src/main/java/lucee/aprint.java +++ b/core/src/main/java/lucee/aprint.java @@ -39,6 +39,7 @@ import org.xml.sax.Attributes; import org.xml.sax.InputSource; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.IOUtil; import lucee.commons.io.SystemUtil; import lucee.commons.io.res.Resource; @@ -56,11 +57,11 @@ */ public class aprint { public static void edate(String value) { - e(SystemOut.FORMAT.format(new Date(System.currentTimeMillis())) + " " + value); + e(FormatUtil.format(SystemOut.FORMAT, new Date(System.currentTimeMillis()), null) + " " + value); } public static void date(String value) { - o(SystemOut.FORMAT.format(new Date(System.currentTimeMillis())) + " " + value); + o(FormatUtil.format(SystemOut.FORMAT, new Date(System.currentTimeMillis()), null) + " " + value); } public static void ds(boolean useOutStream) { diff --git a/core/src/main/java/lucee/commons/date/DateTimeUtil.java b/core/src/main/java/lucee/commons/date/DateTimeUtil.java index b96659bca8..d9bd6606b3 100644 --- a/core/src/main/java/lucee/commons/date/DateTimeUtil.java +++ b/core/src/main/java/lucee/commons/date/DateTimeUtil.java @@ -18,11 +18,13 @@ **/ package lucee.commons.date; -import java.text.SimpleDateFormat; +import java.text.DateFormat; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.Locale; import java.util.TimeZone; +import lucee.commons.i18n.FormatUtil; import lucee.commons.lang.StringUtil; import lucee.runtime.PageContext; import lucee.runtime.engine.ThreadLocalPageContext; @@ -33,20 +35,10 @@ public abstract class DateTimeUtil { - private final static SimpleDateFormat HTTP_TIME_STRING_FORMAT_OLD; - private final static SimpleDateFormat HTTP_TIME_STRING_FORMAT; + private final static DateFormat HTTP_TIME_STRING_FORMAT_OLD = FormatUtil.getDateTimeFormat(Locale.ENGLISH, TimeZoneConstants.GMT, "EE, dd MMM yyyy HH:mm:ss zz"); + private final static DateFormat HTTP_TIME_STRING_FORMAT_NEW = FormatUtil.getDateTimeFormat(Locale.ENGLISH, TimeZoneConstants.UTC, "EE, dd-MMM-yyyy HH:mm:ss zz"); - // public final static SimpleDateFormat DATETIME_FORMAT_LOCAL; - - static { - // DATETIME_FORMAT_LOCAL = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); - - HTTP_TIME_STRING_FORMAT_OLD = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz", Locale.ENGLISH); - HTTP_TIME_STRING_FORMAT_OLD.setTimeZone(TimeZone.getTimeZone("GMT")); - - HTTP_TIME_STRING_FORMAT = new SimpleDateFormat("EE, dd-MMM-yyyy HH:mm:ss zz", Locale.ENGLISH); - HTTP_TIME_STRING_FORMAT.setTimeZone(TimeZoneConstants.UTC); - } + private final static DateTimeFormatter HTTP_TIME_STRING_FORMAT = FormatUtil.getDateTimeFormatter(Locale.ENGLISH, "EE, dd-MMM-yyyy HH:mm:ss zz"); private static final double DAY_MILLIS = 86400000D; private static final long CF_UNIX_OFFSET = 2209161600000L; @@ -278,13 +270,9 @@ public static String toHTTPTimeString(long time, boolean oldFormat) { */ public static String toHTTPTimeString(Date date, boolean oldFormat) { if (oldFormat) { - synchronized (HTTP_TIME_STRING_FORMAT_OLD) { - return StringUtil.replace(HTTP_TIME_STRING_FORMAT_OLD.format(date), "+00:00", "", true); - } - } - synchronized (HTTP_TIME_STRING_FORMAT) { - return StringUtil.replace(HTTP_TIME_STRING_FORMAT.format(date), "+00:00", "", true); + return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT, date, TimeZoneConstants.GMT), "+00:00", "", true); } + return StringUtil.replace(FormatUtil.format(HTTP_TIME_STRING_FORMAT, date, TimeZoneConstants.UTC), "+00:00", "", true); } public static String format(long time, Locale l, TimeZone tz) { diff --git a/core/src/main/java/lucee/commons/i18n/DateFormatPool.java b/core/src/main/java/lucee/commons/i18n/DateFormatPool.java index 66e40058cb..cf923f9031 100644 --- a/core/src/main/java/lucee/commons/i18n/DateFormatPool.java +++ b/core/src/main/java/lucee/commons/i18n/DateFormatPool.java @@ -19,7 +19,7 @@ package lucee.commons.i18n; import java.lang.ref.SoftReference; -import java.text.SimpleDateFormat; +import java.text.DateFormat; import java.util.Date; import java.util.Locale; import java.util.Map; @@ -31,7 +31,7 @@ public final class DateFormatPool { - private final static Map> datax = new ConcurrentHashMap<>(); + private final static Map> datax = new ConcurrentHashMap<>(); public static String format(Locale locale, TimeZone timeZone, String pattern, Date date) { return getSimpleDateFormat(locale, timeZone, pattern).format(date); @@ -45,21 +45,20 @@ public static String format(String pattern, Date date) { return getSimpleDateFormat(null, null, pattern).format(date); } - private static SimpleDateFormat getSimpleDateFormat(Locale locale, TimeZone timeZone, String pattern) { + private static DateFormat getSimpleDateFormat(Locale locale, TimeZone timeZone, String pattern) { if (locale == null) locale = ThreadLocalPageContext.getLocale(); if (timeZone == null) timeZone = ThreadLocalPageContext.getTimeZone(); String key = locale.toString() + '-' + timeZone.getID() + '-' + pattern; - SoftReference ref = datax.get(key); - SimpleDateFormat sdf = ref == null ? null : ref.get(); + SoftReference ref = datax.get(key); + DateFormat sdf = ref == null ? null : ref.get(); if (sdf == null) { synchronized (SystemUtil.createToken("DateFormatPool", key)) { ref = datax.get(key); sdf = ref == null ? null : ref.get(); if (sdf == null) { - sdf = new SimpleDateFormat(pattern, locale); - sdf.setTimeZone(timeZone); + sdf = FormatUtil.getDateTimeFormat(locale, timeZone, pattern); datax.put(key, new SoftReference<>(sdf)); } } diff --git a/core/src/main/java/lucee/commons/i18n/FormatUtil.java b/core/src/main/java/lucee/commons/i18n/FormatUtil.java index 88dd371e42..6fb1bb1fd0 100644 --- a/core/src/main/java/lucee/commons/i18n/FormatUtil.java +++ b/core/src/main/java/lucee/commons/i18n/FormatUtil.java @@ -27,6 +27,7 @@ import java.time.format.DateTimeFormatterBuilder; import java.time.format.FormatStyle; import java.util.ArrayList; +import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Locale; @@ -336,16 +337,16 @@ private static void add24AndRemoveComma(List list, Locale locale, bo DateFormat[] df = list.toArray(new DateFormat[list.size()]); for (int i = 0; i < df.length; i++) { if (df[i] instanceof SimpleDateFormat) { - add24AndRemoveComma(list, (SimpleDateFormat) df[i], locale, isDate, isTime); + add24AndRemoveComma(list, df[i], locale, isDate, isTime); } } } - private static void add24AndRemoveComma(List list, SimpleDateFormat sdf, Locale locale, boolean isDate, boolean isTime) { + private static void add24AndRemoveComma(List list, DateFormat sdf, Locale locale, boolean isDate, boolean isTime) { String p; - List results = new ArrayList(); - p = sdf.toPattern() + ""; + List results = new ArrayList<>(); + p = ((SimpleDateFormat) sdf).toPattern() + ""; // print.e("----- "+p); if (isDate && isTime) { if ((check(results, p, locale, " 'um' ", " "))) { @@ -385,8 +386,8 @@ else if ((check(results, p, locale, "h:mma", "H:mm"))) { } } if (results.size() > 0) { - Iterator it = results.iterator(); - SimpleDateFormat _sdf; + Iterator it = results.iterator(); + DateFormat _sdf; while (it.hasNext()) { _sdf = it.next(); if (!list.contains(_sdf)) { @@ -398,11 +399,12 @@ else if ((check(results, p, locale, "h:mma", "H:mm"))) { } - private static boolean check(List results, String orgPattern, Locale locale, String from, String to) { + private static boolean check(List results, String orgPattern, Locale locale, String from, String to) { int index = orgPattern.indexOf(from); if (index != -1) { String p = StringUtil.replace(orgPattern, from, to, true); - SimpleDateFormat sdf = new SimpleDateFormat(p, locale); + + DateFormat sdf = FormatUtil.getDateTimeFormat(locale, null, p); results.add(sdf); return true; } @@ -424,11 +426,11 @@ private static void addCustom(List list, Locale locale, short format String content = IOUtil.toString(file, (Charset) null); String[] arr = lucee.runtime.type.util.ListUtil.listToStringArray(content, '\n'); String line; - SimpleDateFormat sdf; + DateFormat sdf; for (int i = 0; i < arr.length; i++) { line = arr[i].trim(); if (StringUtil.isEmpty(line)) continue; - sdf = new SimpleDateFormat(line, locale); + sdf = FormatUtil.getDateTimeFormat(locale, null, line); if (!list.contains(sdf)) list.add(sdf); } @@ -481,7 +483,7 @@ public static DateFormat getDateFormat(Locale locale, TimeZone tz, String mask) else if (mask.equalsIgnoreCase("long")) df = DateFormat.getDateInstance(DateFormat.LONG, locale); else if (mask.equalsIgnoreCase("full")) df = DateFormat.getDateInstance(DateFormat.FULL, locale); else { - df = new SimpleDateFormat(mask, locale); + df = FormatUtil.getDateTimeFormat(locale, null, mask); } df.setTimeZone(tz); return df; @@ -518,9 +520,9 @@ public static DateFormat getTimeFormat(Locale locale, TimeZone tz, String mask) else if (mask.equalsIgnoreCase("long")) df = DateFormat.getTimeInstance(DateFormat.LONG, locale); else if (mask.equalsIgnoreCase("full")) df = DateFormat.getTimeInstance(DateFormat.FULL, locale); else { - df = new SimpleDateFormat(mask, locale); + df = locale == null ? new SimpleDateFormat(mask) : new SimpleDateFormat(mask, locale); } - df.setTimeZone(tz); + if (tz != null) df.setTimeZone(tz); return df; } @@ -533,10 +535,28 @@ public static DateFormat getDateTimeFormat(Locale locale, TimeZone tz, String ma else if (mask.equalsIgnoreCase("full")) df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale); else if (mask.equalsIgnoreCase("iso8601")) df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ"); else { - df = new SimpleDateFormat(mask, locale); + df = locale == null ? new SimpleDateFormat(mask) : new SimpleDateFormat(mask, locale); } - df.setTimeZone(tz); + if (tz != null) df.setTimeZone(tz); return df; } + public static DateTimeFormatter getDateTimeFormatter(Locale locale, String mask) { + DateTimeFormatter formatter; + if (mask.equalsIgnoreCase("short")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT); + else if (mask.equalsIgnoreCase("medium")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); + else if (mask.equalsIgnoreCase("long")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG); + else if (mask.equalsIgnoreCase("full")) formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL); + else if (mask.equalsIgnoreCase("iso8601")) formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); + else formatter = DateTimeFormatter.ofPattern(mask); + + if (locale != null) formatter.withLocale(locale); + // if (tz != null) formatter.setTimeZone(tz); + return formatter; + } + + public static String format(DateTimeFormatter formatter, Date date, TimeZone timeZone) { + return date.toInstant().atZone(timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault()).format(formatter); + } + } \ No newline at end of file diff --git a/core/src/main/java/lucee/commons/io/log/LogUtil.java b/core/src/main/java/lucee/commons/io/log/LogUtil.java index 4464791883..977f0dfbab 100644 --- a/core/src/main/java/lucee/commons/io/log/LogUtil.java +++ b/core/src/main/java/lucee/commons/io/log/LogUtil.java @@ -22,6 +22,7 @@ import java.util.Date; import lucee.aprint; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.CharsetUtil; import lucee.commons.io.IOUtil; import lucee.commons.io.res.Resource; @@ -210,7 +211,7 @@ public static void logGlobal(Config config, int level, String type, String msg) log.getParentResource().mkdirs(); log.createNewFile(); } - IOUtil.write(log, SystemOut.FORMAT.format(new Date(System.currentTimeMillis())) + " " + type + " " + msg + "\n", CharsetUtil.UTF8, true); + IOUtil.write(log, FormatUtil.format(SystemOut.FORMAT, new Date(System.currentTimeMillis()), null) + " " + type + " " + msg + "\n", CharsetUtil.UTF8, true); } catch (Exception e) { ERR = null; diff --git a/core/src/main/java/lucee/commons/io/log/log4j2/layout/DataDogLayout.java b/core/src/main/java/lucee/commons/io/log/log4j2/layout/DataDogLayout.java index 4b9f9fecec..033c35025a 100644 --- a/core/src/main/java/lucee/commons/io/log/log4j2/layout/DataDogLayout.java +++ b/core/src/main/java/lucee/commons/io/log/log4j2/layout/DataDogLayout.java @@ -4,12 +4,12 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.text.DateFormat; -import java.text.SimpleDateFormat; import java.util.Date; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.layout.AbstractStringLayout; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.CharsetUtil; import lucee.commons.io.log.LogUtil; import lucee.loader.engine.CFMLEngine; @@ -44,7 +44,7 @@ public DataDogLayout() { super(CharsetUtil.UTF8, new byte[0], new byte[0]); engine = CFMLEngineFactory.getInstance(); caster = engine.getCastUtil(); - format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + format = FormatUtil.getDateTimeFormat(null, null, "yyyy-MM-dd HH:mm:ss"); } @Override diff --git a/core/src/main/java/lucee/commons/lang/SystemOut.java b/core/src/main/java/lucee/commons/lang/SystemOut.java index ff28a5ed70..da0b2c0aba 100755 --- a/core/src/main/java/lucee/commons/lang/SystemOut.java +++ b/core/src/main/java/lucee/commons/lang/SystemOut.java @@ -23,9 +23,10 @@ import java.io.PrintStream; import java.io.PrintWriter; -import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; import java.util.Date; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.DevNullOutputStream; import lucee.commons.io.SystemUtil; import lucee.runtime.PageContext; @@ -34,7 +35,7 @@ public final class SystemOut { - public static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + public static final DateTimeFormatter FORMAT = FormatUtil.getDateTimeFormatter(null, "yyyy-MM-dd HH:mm:ss.S"); /** * logs a value @@ -43,13 +44,13 @@ public final class SystemOut { */ public static void printDate(PrintWriter pw, String value) { long millis = System.currentTimeMillis(); - pw.write(FORMAT.format(new Date(millis)) + " " + value + "\n"); + pw.write(FormatUtil.format(FORMAT, new Date(millis), null) + " " + value + "\n"); pw.flush(); } public static void printDate(PrintWriter pw, Throwable t) { long millis = System.currentTimeMillis(); - pw.write(FORMAT.format(new Date(millis)) + "\n"); + pw.write(FormatUtil.format(FORMAT, new Date(millis), null) + "\n"); t.printStackTrace(pw); pw.write("\n"); pw.flush(); diff --git a/core/src/main/java/lucee/runtime/converter/JSONDateFormat.java b/core/src/main/java/lucee/runtime/converter/JSONDateFormat.java index 935397a879..3d4846e81c 100644 --- a/core/src/main/java/lucee/runtime/converter/JSONDateFormat.java +++ b/core/src/main/java/lucee/runtime/converter/JSONDateFormat.java @@ -19,14 +19,14 @@ package lucee.runtime.converter; import java.lang.ref.SoftReference; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.Locale; import java.util.Map; import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.SystemUtil; import lucee.commons.lang.SerializableObject; import lucee.runtime.engine.ThreadLocalPageContext; @@ -36,7 +36,7 @@ public class JSONDateFormat { public static final String PATTERN_CF = "MMMM, dd yyyy HH:mm:ss Z"; public static final String PATTERN_ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ"; // preferred pattern for json - private static Map> map = new ConcurrentHashMap>(); + private static Map> map = new ConcurrentHashMap<>(); // private static DateFormat format=null; private static Locale locale = Locale.ENGLISH; private final static Object sync = new SerializableObject(); @@ -45,19 +45,18 @@ public static String format(Date date, TimeZone tz, String pattern) { tz = ThreadLocalPageContext.getTimeZone(tz); String id = SystemUtil.createToken(locale.hashCode() + "", tz.getID()); - SoftReference tmp = map.get(id); - DateFormat format = tmp == null ? null : tmp.get(); + SoftReference tmp = map.get(id); + DateTimeFormatter format = tmp == null ? null : tmp.get(); if (format == null) { synchronized (id) { tmp = map.get(id); format = tmp == null ? null : tmp.get(); if (format == null) { - format = new SimpleDateFormat(pattern, locale); - format.setTimeZone(tz); - map.put(id, new SoftReference(format)); + format = FormatUtil.getDateTimeFormatter(locale, pattern); + map.put(id, new SoftReference(format)); } } } - return format.format(date); + return FormatUtil.format(format, date, tz); } } \ No newline at end of file diff --git a/core/src/main/java/lucee/runtime/dump/DumpUtil.java b/core/src/main/java/lucee/runtime/dump/DumpUtil.java index e686beddeb..588c673353 100644 --- a/core/src/main/java/lucee/runtime/dump/DumpUtil.java +++ b/core/src/main/java/lucee/runtime/dump/DumpUtil.java @@ -24,7 +24,7 @@ import java.lang.reflect.Field; import java.nio.charset.Charset; import java.sql.ResultSet; -import java.text.SimpleDateFormat; +import java.text.DateFormat; import java.util.Calendar; import java.util.Date; import java.util.Enumeration; @@ -46,6 +46,7 @@ import org.w3c.dom.NodeList; import lucee.commons.date.TimeZoneUtil; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.res.Resource; import lucee.commons.io.res.util.ResourceUtil; import lucee.commons.lang.CharSet; @@ -116,8 +117,7 @@ public static DumpData toDumpData(Object o, PageContext pageContext, int maxleve if (o instanceof Calendar) { Calendar c = (Calendar) o; - SimpleDateFormat df = new SimpleDateFormat("EE, dd MMM yyyy HH:mm:ss zz", Locale.ENGLISH); - df.setTimeZone(c.getTimeZone()); + DateFormat df = FormatUtil.getDateTimeFormat(Locale.ENGLISH, c.getTimeZone(), "EE, dd MMM yyyy HH:mm:ss zz"); DumpTable table = new DumpTable("date", "#ff9900", "#ffcc00", "#000000"); table.setTitle("java.util.Calendar"); diff --git a/core/src/main/java/lucee/runtime/format/DateFormat.java b/core/src/main/java/lucee/runtime/format/DateFormat.java index aa70885d4d..08f64d2f44 100644 --- a/core/src/main/java/lucee/runtime/format/DateFormat.java +++ b/core/src/main/java/lucee/runtime/format/DateFormat.java @@ -18,7 +18,6 @@ **/ package lucee.runtime.format; -import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -27,6 +26,7 @@ import lucee.commons.date.JREDateTimeUtil; import lucee.commons.date.TimeZoneConstants; +import lucee.commons.i18n.FormatUtil; import lucee.commons.lang.StringUtil; import lucee.runtime.engine.ThreadLocalPageContext; @@ -81,8 +81,7 @@ public String format(long time, String mask, TimeZone tz) { else if (lcMask.equals("long")) return getAsString(calendar, java.text.DateFormat.LONG, tz); else if (lcMask.equals("full")) return getAsString(calendar, java.text.DateFormat.FULL, tz); else if ("iso8601".equals(lcMask) || "iso".equals(lcMask)) { - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - formatter.setTimeZone(tz); + java.text.DateFormat formatter = FormatUtil.getDateTimeFormat(null, tz, "yyyy-MM-dd"); return formatter.format(calendar.getTime()); } diff --git a/core/src/main/java/lucee/runtime/functions/displayFormatting/DateTimeFormat.java b/core/src/main/java/lucee/runtime/functions/displayFormatting/DateTimeFormat.java index 807478aedb..60a35b86ee 100644 --- a/core/src/main/java/lucee/runtime/functions/displayFormatting/DateTimeFormat.java +++ b/core/src/main/java/lucee/runtime/functions/displayFormatting/DateTimeFormat.java @@ -23,6 +23,7 @@ import java.util.Locale; import java.util.TimeZone; +import lucee.commons.i18n.FormatUtil; import lucee.commons.lang.StringUtil; import lucee.runtime.PageContext; import lucee.runtime.engine.ThreadLocalPageContext; @@ -91,9 +92,9 @@ public static String invoke(DateTime datetime, String mask, Locale locale, TimeZ else if ("medium".equalsIgnoreCase(mask)) format = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.MEDIUM, java.text.DateFormat.MEDIUM, locale); else if ("long".equalsIgnoreCase(mask)) format = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.LONG, java.text.DateFormat.LONG, locale); else if ("full".equalsIgnoreCase(mask)) format = java.text.DateFormat.getDateTimeInstance(java.text.DateFormat.FULL, java.text.DateFormat.FULL, locale); - else if ("iso8601".equalsIgnoreCase(mask) || "iso".equalsIgnoreCase(mask)) format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX"); + else if ("iso8601".equalsIgnoreCase(mask) || "iso".equalsIgnoreCase(mask)) format = FormatUtil.getDateTimeFormat(null, null, "yyyy-MM-dd'T'HH:mm:ssXXX"); else if ("isoms".equalsIgnoreCase(mask) || "isoMillis".equalsIgnoreCase(mask) || "javascript".equalsIgnoreCase(mask)) - format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); + format = FormatUtil.getDateTimeFormat(null, null, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); else if ("epoch".equalsIgnoreCase(mask)) { String gettime = String.valueOf(datetime.getTime() / 1000); String epoch = gettime.toString(); @@ -105,12 +106,12 @@ else if ("epochms".equalsIgnoreCase(mask)) { return epoch; } else { - SimpleDateFormat sdf; - format = sdf = new SimpleDateFormat(convertMask(mask), locale); + java.text.DateFormat sdf; + format = sdf = FormatUtil.getDateTimeFormat(locale, null, convertMask(mask)); if (mask != null && StringUtil.indexOfIgnoreCase(mask, "tt") == -1 && StringUtil.indexOfIgnoreCase(mask, "t") != -1) { DateFormatSymbols dfs = new DateFormatSymbols(locale); dfs.setAmPmStrings(AP); - sdf.setDateFormatSymbols(dfs); + ((SimpleDateFormat) sdf).setDateFormatSymbols(dfs); } } format.setTimeZone(tz); diff --git a/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java b/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java index 07dd86c013..20eee2c297 100644 --- a/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java +++ b/core/src/main/java/lucee/runtime/functions/international/LSParseDateTime.java @@ -21,13 +21,14 @@ */ package lucee.runtime.functions.international; +import java.text.DateFormat; import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; import java.util.TimeZone; import lucee.commons.date.TimeZoneUtil; +import lucee.commons.i18n.FormatUtil; import lucee.commons.lang.StringUtil; import lucee.runtime.PageContext; import lucee.runtime.engine.ThreadLocalPageContext; @@ -75,8 +76,7 @@ private static lucee.runtime.type.dt.DateTime _call(PageContext pc, Object oDate // with java based format tz = ThreadLocalPageContext.getTimeZone(tz); if (locale == null) locale = pc.getLocale(); - SimpleDateFormat df = new SimpleDateFormat(format, locale); - df.setTimeZone(tz); + DateFormat df = FormatUtil.getDateTimeFormat(locale, tz, format); try { return new DateTimeImpl(df.parse(strDate)); } diff --git a/core/src/main/java/lucee/runtime/net/smtp/SMTPClient.java b/core/src/main/java/lucee/runtime/net/smtp/SMTPClient.java index 5453e15fb7..550c93ea4f 100644 --- a/core/src/main/java/lucee/runtime/net/smtp/SMTPClient.java +++ b/core/src/main/java/lucee/runtime/net/smtp/SMTPClient.java @@ -24,7 +24,7 @@ import java.lang.ref.SoftReference; import java.net.URL; import java.nio.charset.Charset; -import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -56,6 +56,7 @@ import lucee.commons.activation.ResourceDataSource; import lucee.commons.digest.MD5; +import lucee.commons.i18n.FormatUtil; import lucee.commons.io.CharsetUtil; import lucee.commons.io.SystemUtil; import lucee.commons.io.log.Log; @@ -120,7 +121,7 @@ public final class SMTPClient implements Serializable { private static final String MESSAGE_ID = "Message-ID"; // private static final SerializableObject LOCK = new SerializableObject(); - private static Map> formatters = new ConcurrentHashMap>(); + private static Map> formatters = new ConcurrentHashMap<>(); // private static final int PORT = 25; private int spool = SPOOL_UNDEFINED; @@ -168,14 +169,13 @@ public final class SMTPClient implements Serializable { public static String getNow(TimeZone tz) { tz = ThreadLocalPageContext.getTimeZone(tz); - SoftReference tmp = formatters.get(tz); - SimpleDateFormat df = tmp == null ? null : tmp.get(); + SoftReference tmp = formatters.get(tz); + DateTimeFormatter df = tmp == null ? null : tmp.get(); if (df == null) { - df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z (z)", Locale.US); - df.setTimeZone(tz); - formatters.put(tz, new SoftReference(df)); + df = FormatUtil.getDateTimeFormatter(Locale.US, "EEE, d MMM yyyy HH:mm:ss Z (z)"); + formatters.put(tz, new SoftReference(df)); } - return df.format(new Date()); + return FormatUtil.format(df, new Date(), tz); } public void setSpoolenable(boolean spoolenable) { diff --git a/core/src/main/java/lucee/runtime/type/dt/DateImpl.java b/core/src/main/java/lucee/runtime/type/dt/DateImpl.java index 94792cc1fa..f716172a14 100755 --- a/core/src/main/java/lucee/runtime/type/dt/DateImpl.java +++ b/core/src/main/java/lucee/runtime/type/dt/DateImpl.java @@ -19,10 +19,11 @@ package lucee.runtime.type.dt; import java.math.BigDecimal; -import java.text.SimpleDateFormat; +import java.text.DateFormat; import java.util.Locale; import lucee.commons.date.DateTimeUtil; +import lucee.commons.i18n.FormatUtil; import lucee.runtime.PageContext; import lucee.runtime.dump.DumpData; import lucee.runtime.dump.DumpProperties; @@ -38,7 +39,7 @@ */ public final class DateImpl extends Date implements SimpleValue { - private static SimpleDateFormat luceeFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); + private static DateFormat luceeFormatter = FormatUtil.getDateTimeFormat(Locale.US, null, "yyyy-MM-dd"); // private TimeZone timezone; diff --git a/core/src/main/java/lucee/runtime/type/dt/TimeImpl.java b/core/src/main/java/lucee/runtime/type/dt/TimeImpl.java index c80f99fd7a..7412bafa71 100755 --- a/core/src/main/java/lucee/runtime/type/dt/TimeImpl.java +++ b/core/src/main/java/lucee/runtime/type/dt/TimeImpl.java @@ -19,10 +19,11 @@ package lucee.runtime.type.dt; -import java.text.SimpleDateFormat; +import java.time.format.DateTimeFormatter; import java.util.Locale; import lucee.commons.date.DateTimeUtil; +import lucee.commons.i18n.FormatUtil; import lucee.runtime.PageContext; import lucee.runtime.dump.DumpData; import lucee.runtime.dump.DumpProperties; @@ -39,7 +40,7 @@ */ public final class TimeImpl extends Time implements SimpleValue { - private static SimpleDateFormat luceeFormatter = new SimpleDateFormat("HH:mm:ss", Locale.US); + private static DateTimeFormatter luceeFormatter = FormatUtil.getDateTimeFormatter(Locale.US, "HH:mm:ss"); public TimeImpl() { super(System.currentTimeMillis()); @@ -75,10 +76,7 @@ public TimeImpl(java.util.Date date) { @Override public String castToString() { - synchronized (luceeFormatter) { - luceeFormatter.setTimeZone(ThreadLocalPageContext.getTimeZone()); - return "{t '" + luceeFormatter.format(this) + "'}"; - } + return "{t '" + FormatUtil.format(luceeFormatter, this, ThreadLocalPageContext.getTimeZone()) + "'}"; } @Override @@ -89,8 +87,7 @@ public String toString() { @Override public String castToString(String defaultValue) { synchronized (luceeFormatter) { - luceeFormatter.setTimeZone(ThreadLocalPageContext.getTimeZone()); - return "{t '" + luceeFormatter.format(this) + "'}"; + return "{t '" + FormatUtil.format(luceeFormatter, this, ThreadLocalPageContext.getTimeZone()) + "'}"; } } diff --git a/core/src/main/java/org/apache/taglibs/datetime/FormatTag.java b/core/src/main/java/org/apache/taglibs/datetime/FormatTag.java index de3e5b0cad..5fed5fed16 100644 --- a/core/src/main/java/org/apache/taglibs/datetime/FormatTag.java +++ b/core/src/main/java/org/apache/taglibs/datetime/FormatTag.java @@ -18,6 +18,7 @@ **/ package org.apache.taglibs.datetime; +import java.text.DateFormat; import java.text.DateFormatSymbols; import java.text.SimpleDateFormat; import java.util.Date; @@ -27,6 +28,7 @@ import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; +import lucee.commons.i18n.FormatUtil; import lucee.runtime.PageContext; import lucee.runtime.exp.ApplicationException; import lucee.runtime.exp.PageException; @@ -106,7 +108,7 @@ public final int doEndTag() throws PageException { if (output_date != null) { // Get the pattern to use - SimpleDateFormat sdf; + DateFormat sdf; String pat = pattern; if (pat == null && patternid != null) { @@ -115,8 +117,9 @@ public final int doEndTag() throws PageException { } if (pat == null) { - sdf = new SimpleDateFormat(); - pat = sdf.toPattern(); + SimpleDateFormat tmp; + sdf = tmp = new SimpleDateFormat(); + pat = tmp.toPattern(); } // Get a DateFormatSymbols @@ -134,16 +137,16 @@ public final int doEndTag() throws PageException { throw new ApplicationException("datetime format tag could not find locale for localeRef \"" + localeRef + "\"."); } - sdf = new SimpleDateFormat(pat, locale); + sdf = FormatUtil.getDateTimeFormat(locale, null, pat); } else if (locale_flag) { - sdf = new SimpleDateFormat(pat, pageContext.getRequest().getLocale()); + sdf = FormatUtil.getDateTimeFormat(pageContext.getRequest().getLocale(), null, pat); } else if (symbols != null) { sdf = new SimpleDateFormat(pat, symbols); } else { - sdf = new SimpleDateFormat(pat); + sdf = FormatUtil.getDateTimeFormat(null, null, pat); } // See if there is a timeZone diff --git a/loader/build.xml b/loader/build.xml index 22ec823243..0723b3d007 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index 5b7f288a98..2973604bb9 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.2.0.121-SNAPSHOT + 6.2.0.122-SNAPSHOT jar Lucee Loader Build