mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
support custom date format parse
This commit is contained in:
parent
2f4544bc23
commit
85e9bc11c2
@ -10,6 +10,7 @@
|
||||
* 【core 】 CsvWriter增加writeBeans方法(pr#345@Gitee)
|
||||
* 【core 】 新增JAXBUtil(pr#346@Gitee)
|
||||
* 【poi 】 ExcelWriter新增setColumnStyleIfHasData和setRowStyleIfHasData(pr#347@Gitee)
|
||||
* 【json 】 用户自定义日期时间格式时,解析也读取此格式
|
||||
|
||||
### 🐞Bug修复
|
||||
* 【json 】 修复XML转义字符的问题(issue#I3XH09@Gitee)
|
||||
|
@ -1,11 +1,11 @@
|
||||
package cn.hutool.core.getter;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Date;
|
||||
|
||||
import cn.hutool.core.convert.Convert;
|
||||
|
||||
/**
|
||||
* 基本类型的getter接口抽象实现,所有类型的值获取都是通过将getObj获得的值转换而来<br>
|
||||
* 用户只需实现getObj方法即可,其他类型将会从Object结果中转换
|
||||
|
@ -1,7 +1,13 @@
|
||||
package cn.hutool.json;
|
||||
|
||||
import cn.hutool.core.bean.OptionalBean;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.core.convert.ConvertException;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.getter.OptNullBasicTypeFromObjectGetter;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 用于JSON的Getter类,提供各种类型的Getter方法
|
||||
@ -105,6 +111,26 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return (null == obj) ? null : obj.toBean(beanType);
|
||||
}
|
||||
|
||||
@Override
|
||||
default Date getDate(K key, Date defaultValue){
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = getStr(key);
|
||||
if(null == str){
|
||||
return defaultValue;
|
||||
}
|
||||
return DateUtil.parse(str, format);
|
||||
}
|
||||
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
return Convert.toDate(obj, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象<br>
|
||||
* 转换失败或抛出异常
|
||||
|
@ -429,6 +429,27 @@ public class JSONObjectTest {
|
||||
Assert.assertEquals("{\"date\":[\"2020-06-05 11:16:11\"],\"bbb\":[\"222\"],\"aaa\":[\"123\"]}", json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDateFormatTest2(){
|
||||
JSONConfig jsonConfig = JSONConfig.create();
|
||||
jsonConfig.setDateFormat("yyyy#MM#dd");
|
||||
jsonConfig.setOrder(true);
|
||||
|
||||
Date date = DateUtil.parse("2020-06-05 11:16:11");
|
||||
JSONObject json = new JSONObject(jsonConfig);
|
||||
json.set("date", date);
|
||||
json.set("bbb", "222");
|
||||
json.set("aaa", "123");
|
||||
|
||||
String jsonStr = "{\"date\":\"2020#06#05\",\"bbb\":\"222\",\"aaa\":\"123\"}";
|
||||
|
||||
Assert.assertEquals(jsonStr, json.toString());
|
||||
|
||||
// 解析测试
|
||||
final JSONObject parse = JSONUtil.parseObj(jsonStr, jsonConfig);
|
||||
Assert.assertEquals(DateUtil.beginOfDay(date), parse.getDate("date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getTimestampTest(){
|
||||
String timeStr = "1970-01-01 00:00:00";
|
||||
|
@ -74,6 +74,25 @@ public class JWTValidator {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查JWT的以下三两个时间:
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link JWTPayload#NOT_BEFORE}:被检查时间必须晚于生效时间</li>
|
||||
* <li>{@link JWTPayload#EXPIRES_AT}:被检查时间必须早于失效时间</li>
|
||||
* <li>{@link JWTPayload#ISSUED_AT}:签发时间必须早于失效时间</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* 如果某个时间没有设置,则不检查(表示无限制)
|
||||
*
|
||||
* @return this
|
||||
* @throws ValidateException 验证失败的异常
|
||||
* @since 5.7.3
|
||||
*/
|
||||
public JWTValidator validateDate() throws ValidateException {
|
||||
return validateDate(DateUtil.beginOfSecond(DateUtil.date()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查JWT的以下三两个时间:
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user