mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
support custom date format parse
This commit is contained in:
@@ -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>
|
||||
* 转换失败或抛出异常
|
||||
|
Reference in New Issue
Block a user