mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add util
This commit is contained in:
@@ -4,9 +4,11 @@ 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.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.getter.OptNullBasicTypeFromObjectGetter;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
@@ -135,6 +137,37 @@ public interface JSONGetter<K> extends OptNullBasicTypeFromObjectGetter<K> {
|
||||
return Convert.toDate(obj, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取{@link LocalDateTime}类型值
|
||||
*
|
||||
* @param key 键
|
||||
* @param defaultValue 默认值
|
||||
* @return {@link LocalDateTime}
|
||||
* @since 5.7.7
|
||||
*/
|
||||
default LocalDateTime getLocalDateTime(K key, LocalDateTime defaultValue){
|
||||
// 默认转换
|
||||
final Object obj = getObj(key);
|
||||
if (null == obj) {
|
||||
return defaultValue;
|
||||
}
|
||||
if(obj instanceof LocalDateTime){
|
||||
return (LocalDateTime) obj;
|
||||
}
|
||||
|
||||
String format = OptionalBean.ofNullable(getConfig()).getBean(JSONConfig::getDateFormat).get();
|
||||
if(StrUtil.isNotBlank(format)){
|
||||
// 用户指定了日期格式,获取日期属性时使用对应格式
|
||||
final String str = Convert.toStr(obj);
|
||||
if(null == str){
|
||||
return defaultValue;
|
||||
}
|
||||
return LocalDateTimeUtil.parse(str, format);
|
||||
}
|
||||
|
||||
return Convert.toLocalDateTime(obj, defaultValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定类型的对象<br>
|
||||
* 转换失败或抛出异常
|
||||
|
Reference in New Issue
Block a user