mirror of
https://gitee.com/egzosn/pay-java-parent.git
synced 2026-05-17 04:02:52 +08:00
日期工具类优化
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
package com.egzosn.pay.common.util;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import org.apache.http.util.Args;
|
||||
|
||||
import java.lang.ref.SoftReference;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 日期转换运算工具
|
||||
@@ -14,27 +15,56 @@ import java.util.TimeZone;
|
||||
* </pre>
|
||||
*/
|
||||
public final class DateUtils {
|
||||
public static final DateFormat YYYY_MM_DD_HH_MM_SS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
public static final DateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd");
|
||||
public static final DateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd");
|
||||
public static final DateFormat YYYYMMDDHHMMSS = new SimpleDateFormat("yyyyMMddHHmmss");
|
||||
public static final DateFormat MMDD = new SimpleDateFormat("MMdd");
|
||||
|
||||
static {
|
||||
TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
|
||||
YYYY_MM_DD_HH_MM_SS.setTimeZone(timeZone);
|
||||
YYYY_MM_DD.setTimeZone(timeZone);
|
||||
YYYYMMDD.setTimeZone(timeZone);
|
||||
YYYYMMDDHHMMSS.setTimeZone(timeZone);
|
||||
MMDD.setTimeZone(timeZone);
|
||||
static final class DateFormatHolder {
|
||||
private static final ThreadLocal<SoftReference<Map<String, SimpleDateFormat>>> THREADLOCAL_FORMATS = new ThreadLocal();
|
||||
|
||||
DateFormatHolder() {
|
||||
}
|
||||
|
||||
public static SimpleDateFormat formatFor(String pattern) {
|
||||
SoftReference ref = (SoftReference)THREADLOCAL_FORMATS.get();
|
||||
Object formats = ref == null?null:(Map)ref.get();
|
||||
if(formats == null) {
|
||||
formats = new HashMap();
|
||||
THREADLOCAL_FORMATS.set(new SoftReference(formats));
|
||||
}
|
||||
|
||||
SimpleDateFormat format = (SimpleDateFormat)((Map)formats).get(pattern);
|
||||
|
||||
if(format == null) {
|
||||
format = new SimpleDateFormat(pattern);
|
||||
format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
|
||||
((Map)formats).put(pattern, format);
|
||||
}
|
||||
|
||||
return format;
|
||||
}
|
||||
|
||||
public static void clearThreadLocal() {
|
||||
THREADLOCAL_FORMATS.remove();
|
||||
}
|
||||
}
|
||||
|
||||
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final String YYYY_MM_DD = "yyyy-MM-dd";
|
||||
public static final String YYYYMMDD = "yyyyMMdd";
|
||||
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
|
||||
public static final String MMDD = "MMdd";
|
||||
|
||||
|
||||
public static String formatDate(Date date, String pattern) {
|
||||
Args.notNull(date, "Date");
|
||||
Args.notNull(pattern, "Pattern");
|
||||
SimpleDateFormat formatFor = DateFormatHolder.formatFor(YYYY_MM_DD);
|
||||
return formatFor.format(System.currentTimeMillis());
|
||||
}
|
||||
public static final String format(Date date){
|
||||
return YYYY_MM_DD_HH_MM_SS.format(date);
|
||||
return formatDate(date, YYYY_MM_DD_HH_MM_SS);
|
||||
}
|
||||
|
||||
public static final String formatDay(Date date){
|
||||
return YYYY_MM_DD.format(date);
|
||||
return formatDate(date, YYYY_MM_DD);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,8 +12,6 @@ import com.egzosn.pay.common.util.str.StringUtils;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.security.cert.*;
|
||||
import java.sql.Timestamp;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
@@ -232,9 +233,9 @@ public class UnionPayService extends BasePayService<UnionPayConfigStorage> {
|
||||
// 此时间建议取支付时的北京时间加15分钟。
|
||||
// 超过超时时间调查询接口应答origRespCode不是A6或者00的就可以判断为失败。
|
||||
if (null != order.getExpirationTime()) {
|
||||
params.put(SDKConstants.param_payTimeout, DateUtils.YYYYMMDDHHMMSS.format(order.getExpirationTime()));
|
||||
params.put(SDKConstants.param_payTimeout, DateUtils.formatDate(order.getExpirationTime(), DateUtils.YYYYMMDDHHMMSS));
|
||||
} else {
|
||||
params.put(SDKConstants.param_payTimeout, DateUtils.YYYYMMDDHHMMSS.format(System.currentTimeMillis() + 30 * 60 * 1000));
|
||||
params.put(SDKConstants.param_payTimeout, DateUtils.formatDate(new Timestamp(System.currentTimeMillis() + 30 * 60 * 1000), DateUtils.YYYYMMDDHHMMSS));
|
||||
}
|
||||
params.put(SDKConstants.param_frontUrl, payConfigStorage.getReturnUrl());
|
||||
break;
|
||||
@@ -589,7 +590,7 @@ public class UnionPayService extends BasePayService<UnionPayConfigStorage> {
|
||||
Map<String, Object> params = this.getCommonParam();
|
||||
UnionTransactionType.FILE_TRANSFER.convertMap(params);
|
||||
|
||||
params.put(SDKConstants.param_settleDate, DateUtils.MMDD.format(billDate));
|
||||
params.put(SDKConstants.param_settleDate, DateUtils.formatDate(billDate, DateUtils.MMDD));
|
||||
params.put(SDKConstants.param_fileType, billType);
|
||||
params.remove(SDKConstants.param_backUrl);
|
||||
params.remove(SDKConstants.param_currencyCode);
|
||||
|
||||
@@ -203,8 +203,8 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
|
||||
parameters.put("notify_url", payConfigStorage.getNotifyUrl());
|
||||
parameters.put("trade_type", order.getTransactionType().getType());
|
||||
if (null != order.getExpirationTime()){
|
||||
parameters.put("time_start", DateUtils.YYYYMMDDHHMMSS.format(new Date()));
|
||||
parameters.put("time_expire", DateUtils.YYYYMMDDHHMMSS.format(order.getExpirationTime()));
|
||||
parameters.put("time_start", DateUtils.formatDate(new Date(), DateUtils.YYYYMMDDHHMMSS));
|
||||
parameters.put("time_expire", DateUtils.formatDate(order.getExpirationTime(), DateUtils.YYYYMMDDHHMMSS));
|
||||
}
|
||||
((WxTransactionType) order.getTransactionType()).setAttribute(parameters, order);
|
||||
|
||||
@@ -530,7 +530,7 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
|
||||
parameters.put("bill_type", billType);
|
||||
//目前只支持日账单
|
||||
|
||||
parameters.put("bill_date", DateUtils.YYYYMMDD.format(billDate));
|
||||
parameters.put("bill_date", DateUtils.formatDate(billDate, DateUtils.YYYYMMDD));
|
||||
|
||||
//设置签名
|
||||
setSign(parameters);
|
||||
|
||||
Reference in New Issue
Block a user