mirror of
https://github.com/chillzhuang/blade-tool
synced 2024-11-05 10:09:32 +08:00
⚡ 根据P3C优化代码
This commit is contained in:
parent
a623b387a5
commit
8abdf22c94
@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* {@link SimpleDateFormat}的线程安全包装器。
|
||||
* 不使用ThreadLocal,创建足够的SimpleDateFormat对象来满足并发性要求。
|
||||
* </p>
|
||||
*
|
||||
* @author L.cm
|
||||
*/
|
||||
public class ConcurrentDateFormat {
|
||||
private final String format;
|
||||
private final Locale locale;
|
||||
private final TimeZone timezone;
|
||||
private final Queue<SimpleDateFormat> 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;
|
||||
}
|
||||
}
|
@ -0,0 +1,216 @@
|
||||
/**
|
||||
* Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com).
|
||||
* <p>
|
||||
* 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
|
||||
* <p>
|
||||
* http://www.gnu.org/licenses/lgpl.html
|
||||
* <p>
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user