From 8abdf22c9469519e6655952a78809e3ae3305943 Mon Sep 17 00:00:00 2001 From: smallchill Date: Thu, 27 Dec 2018 17:36:29 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E6=A0=B9=E6=8D=AEP3C=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/utils/ConcurrentDateFormat.java | 86 +++++++ .../springblade/core/tool/utils/DateUtil.java | 216 ++++++++++++++++++ 2 files changed, 302 insertions(+) create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/utils/ConcurrentDateFormat.java create mode 100644 blade-core-tool/src/main/java/org/springblade/core/tool/utils/DateUtil.java diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ConcurrentDateFormat.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ConcurrentDateFormat.java new file mode 100644 index 0000000..088efa8 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ConcurrentDateFormat.java @@ -0,0 +1,86 @@ +/** + * Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.tool.utils; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; +import java.util.Queue; +import java.util.TimeZone; +import java.util.concurrent.ConcurrentLinkedQueue; + +/** + * 参考tomcat8中的并发DateFormat + *

+ * {@link SimpleDateFormat}的线程安全包装器。 + * 不使用ThreadLocal,创建足够的SimpleDateFormat对象来满足并发性要求。 + *

+ * + * @author L.cm + */ +public class ConcurrentDateFormat { + private final String format; + private final Locale locale; + private final TimeZone timezone; + private final Queue queue = new ConcurrentLinkedQueue<>(); + + private ConcurrentDateFormat(String format, Locale locale, TimeZone timezone) { + this.format = format; + this.locale = locale; + this.timezone = timezone; + SimpleDateFormat initial = createInstance(); + queue.add(initial); + } + + public static ConcurrentDateFormat of(String format) { + return new ConcurrentDateFormat(format, Locale.getDefault(), TimeZone.getDefault()); + } + + public static ConcurrentDateFormat of(String format, TimeZone timezone) { + return new ConcurrentDateFormat(format, Locale.getDefault(), timezone); + } + + public static ConcurrentDateFormat of(String format, Locale locale, TimeZone timezone) { + return new ConcurrentDateFormat(format, locale, timezone); + } + + public String format(Date date) { + SimpleDateFormat sdf = queue.poll(); + if (sdf == null) { + sdf = createInstance(); + } + String result = sdf.format(date); + queue.add(sdf); + return result; + } + + public Date parse(String source) throws ParseException { + SimpleDateFormat sdf = queue.poll(); + if (sdf == null) { + sdf = createInstance(); + } + Date result = sdf.parse(source); + queue.add(sdf); + return result; + } + + private SimpleDateFormat createInstance() { + SimpleDateFormat sdf = new SimpleDateFormat(format, locale); + sdf.setTimeZone(timezone); + return sdf; + } +} diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DateUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DateUtil.java new file mode 100644 index 0000000..0e8a5b5 --- /dev/null +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/DateUtil.java @@ -0,0 +1,216 @@ +/** + * Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.tool.utils; + +import org.springframework.util.Assert; + +import java.text.ParseException; +import java.util.Calendar; +import java.util.Date; + +/** + * 日期工具类 + * + * @author L.cm + */ +public class DateUtil { + + /** + * 设置年 + * + * @param date 时间 + * @param amount 年数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setYears(Date date, int amount) { + return set(date, Calendar.YEAR, amount); + } + + /** + * 设置月 + * + * @param date 时间 + * @param amount 月数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setMonths(Date date, int amount) { + return set(date, Calendar.MONTH, amount); + } + + /** + * 设置周 + * + * @param date 时间 + * @param amount 周数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setWeeks(Date date, int amount) { + return set(date, Calendar.WEEK_OF_YEAR, amount); + } + + /** + * 设置天 + * + * @param date 时间 + * @param amount 天数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setDays(Date date, int amount) { + return set(date, Calendar.DATE, amount); + } + + /** + * 设置小时 + * + * @param date 时间 + * @param amount 小时数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setHours(Date date, int amount) { + return set(date, Calendar.HOUR_OF_DAY, amount); + } + + /** + * 设置分钟 + * + * @param date 时间 + * @param amount 分钟数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setMinutes(Date date, int amount) { + return set(date, Calendar.MINUTE, amount); + } + + /** + * 设置秒 + * + * @param date 时间 + * @param amount 秒数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setSeconds(Date date, int amount) { + return set(date, Calendar.SECOND, amount); + } + + /** + * 设置毫秒 + * + * @param date 时间 + * @param amount 毫秒数,-1表示减少 + * @return 设置后的时间 + */ + public static Date setMilliseconds(Date date, int amount) { + return set(date, Calendar.MILLISECOND, amount); + } + + /** + * 设置日期属性 + * + * @param date 时间 + * @param calendarField 更改的属性 + * @param amount 更改数,-1表示减少 + * @return 设置后的时间 + */ + private static Date set(Date date, int calendarField, int amount) { + Assert.notNull(date, "The date must not be null"); + Calendar c = Calendar.getInstance(); + c.setLenient(false); + c.setTime(date); + c.add(calendarField, amount); + return c.getTime(); + } + + public static final String PATTERN_DATETIME = "yyyy-MM-dd HH:mm:ss"; + public static final String PATTERN_DATE = "yyyy-MM-dd"; + public static final String PATTERN_TIME = "HH:mm:ss"; + + public static final ConcurrentDateFormat DATETIME_FORMAT = ConcurrentDateFormat.of(PATTERN_DATETIME); + public static final ConcurrentDateFormat DATE_FORMAT = ConcurrentDateFormat.of(PATTERN_DATE); + public static final ConcurrentDateFormat TIME_FORMAT = ConcurrentDateFormat.of(PATTERN_TIME); + + /** + * 日期时间格式化 + * + * @param date 时间 + * @return 格式化后的时间 + */ + public static String formatDateTime(Date date) { + return DATETIME_FORMAT.format(date); + } + + /** + * 日期格式化 + * + * @param date 时间 + * @return 格式化后的时间 + */ + public static String formatDate(Date date) { + return DATE_FORMAT.format(date); + } + + /** + * 时间格式化 + * + * @param date 时间 + * @return 格式化后的时间 + */ + public static String formatTime(Date date) { + return TIME_FORMAT.format(date); + } + + /** + * 日期格式化 + * + * @param date 时间 + * @param pattern 表达式 + * @return 格式化后的时间 + */ + public static String format(Date date, String pattern) { + return ConcurrentDateFormat.of(pattern).format(date); + } + + /** + * 将字符串转换为时间 + * + * @param dateStr 时间字符串 + * @param pattern 表达式 + * @return 时间 + */ + public static Date parse(String dateStr, String pattern) { + ConcurrentDateFormat format = ConcurrentDateFormat.of(pattern); + try { + return format.parse(dateStr); + } catch (ParseException e) { + throw Exceptions.unchecked(e); + } + } + + /** + * 将字符串转换为时间 + * + * @param dateStr 时间字符串 + * @param format ConcurrentDateFormat + * @return 时间 + */ + public static Date parse(String dateStr, ConcurrentDateFormat format) { + try { + return format.parse(dateStr); + } catch (ParseException e) { + throw Exceptions.unchecked(e); + } + } + +}