Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mica
mica
提交
c9f60302
mica
项目概览
mica
/
mica
通知
10
Star
2
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
mica
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c9f60302
编写于
8月 13, 2020
作者:
如梦技术
🐛
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
✨
优化 mica-core DateUtil。
上级
5a8e29d9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
116 addition
and
15 deletion
+116
-15
mica-core/src/main/java/net/dreamlu/mica/core/format/FastDateFormat.java
...ain/java/net/dreamlu/mica/core/format/FastDateFormat.java
+82
-0
mica-core/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java
...e/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java
+32
-13
mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
...e/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
+2
-2
未找到文件。
mica-core/src/main/java/net/dreamlu/mica/core/format/FastDateFormat.java
0 → 100644
浏览文件 @
c9f60302
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* 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
net.dreamlu.mica.core.format
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.SneakyThrows
;
import
net.dreamlu.mica.core.utils.DateUtil
;
import
java.text.DateFormat
;
import
java.text.FieldPosition
;
import
java.text.ParsePosition
;
import
java.time.format.DateTimeFormatter
;
import
java.util.Date
;
import
java.util.Locale
;
/**
* 线程安全、高性能的 DateFormat
*
* <p>
* 用于某些参数为 DateFormat 的组件中,对于业务代码推荐直接使用 DateUtil 性能会更好。
* </p>
*
* @author L.cm
*/
@Getter
@Setter
public
class
FastDateFormat
extends
DateFormat
{
private
static
final
java
.
lang
.
reflect
.
Field
FIELD
=
getToStringCacheField
();
private
final
DateTimeFormatter
formatter
;
public
FastDateFormat
(
DateTimeFormatter
formatter
)
{
this
.
formatter
=
formatter
;
}
public
FastDateFormat
(
String
pattern
)
{
this
(
DateTimeFormatter
.
ofPattern
(
pattern
));
}
public
FastDateFormat
(
String
pattern
,
Locale
locale
)
{
this
(
DateTimeFormatter
.
ofPattern
(
pattern
,
locale
));
}
@SneakyThrows
@Override
public
StringBuffer
format
(
Date
date
,
StringBuffer
stringBuffer
,
FieldPosition
fieldPosition
)
{
FIELD
.
set
(
stringBuffer
,
DateUtil
.
format
(
date
,
formatter
));
return
stringBuffer
;
}
@Override
public
Date
parse
(
String
source
)
{
return
DateUtil
.
parse
(
source
,
formatter
);
}
@Override
public
Date
parse
(
String
dateStr
,
ParsePosition
parsePosition
)
{
return
null
;
}
@SneakyThrows
private
static
java
.
lang
.
reflect
.
Field
getToStringCacheField
()
{
java
.
lang
.
reflect
.
Field
field
=
StringBuffer
.
class
.
getDeclaredField
(
"toStringCache"
);
field
.
setAccessible
(
true
);
return
field
;
}
}
mica-core/src/main/java/net/dreamlu/mica/core/utils/DateUtil.java
浏览文件 @
c9f60302
...
@@ -310,10 +310,7 @@ public class DateUtil {
...
@@ -310,10 +310,7 @@ public class DateUtil {
*/
*/
@Nullable
@Nullable
public
static
String
formatDateTime
(
@Nullable
Date
date
)
{
public
static
String
formatDateTime
(
@Nullable
Date
date
)
{
if
(
date
==
null
)
{
return
format
(
date
,
DATETIME_FORMATTER
);
return
null
;
}
return
DATETIME_FORMATTER
.
format
(
date
.
toInstant
());
}
}
/**
/**
...
@@ -324,10 +321,7 @@ public class DateUtil {
...
@@ -324,10 +321,7 @@ public class DateUtil {
*/
*/
@Nullable
@Nullable
public
static
String
formatDate
(
@Nullable
Date
date
)
{
public
static
String
formatDate
(
@Nullable
Date
date
)
{
if
(
date
==
null
)
{
return
format
(
date
,
DATE_FORMATTER
);
return
null
;
}
return
DATE_FORMATTER
.
format
(
date
.
toInstant
());
}
}
/**
/**
...
@@ -338,10 +332,7 @@ public class DateUtil {
...
@@ -338,10 +332,7 @@ public class DateUtil {
*/
*/
@Nullable
@Nullable
public
static
String
formatTime
(
@Nullable
Date
date
)
{
public
static
String
formatTime
(
@Nullable
Date
date
)
{
if
(
date
==
null
)
{
return
format
(
date
,
TIME_FORMATTER
);
return
null
;
}
return
TIME_FORMATTER
.
format
(
date
.
toInstant
());
}
}
/**
/**
...
@@ -353,10 +344,38 @@ public class DateUtil {
...
@@ -353,10 +344,38 @@ public class DateUtil {
*/
*/
@Nullable
@Nullable
public
static
String
format
(
@Nullable
Date
date
,
String
pattern
)
{
public
static
String
format
(
@Nullable
Date
date
,
String
pattern
)
{
return
format
(
date
,
DateTimeFormatter
.
ofPattern
(
pattern
));
}
/**
* 日期格式化
*
* @param date 时间
* @param formatter 格式化
* @return 格式化后的时间
*/
@Nullable
public
static
String
format
(
@Nullable
Date
date
,
DateTimeFormatter
formatter
)
{
if
(
date
==
null
)
{
if
(
date
==
null
)
{
return
null
;
return
null
;
}
}
return
DateTimeFormatter
.
ofPattern
(
pattern
).
withZone
(
ZoneId
.
systemDefault
()).
format
(
date
.
toInstant
());
return
format
(
date
.
toInstant
(),
formatter
);
}
/**
* 日期格式化
*
* @param instant 时间
* @param formatter 格式化
* @return 格式化后的时间
*/
@Nullable
public
static
String
format
(
Instant
instant
,
DateTimeFormatter
formatter
)
{
ZoneId
zone
=
formatter
.
getZone
();
if
(
zone
==
null
)
{
return
formatter
.
withZone
(
ZoneId
.
systemDefault
()).
format
(
instant
);
}
return
formatter
.
format
(
instant
);
}
}
/**
/**
...
...
mica-core/src/main/java/net/dreamlu/mica/core/utils/JsonUtil.java
浏览文件 @
c9f60302
...
@@ -26,12 +26,12 @@ import com.fasterxml.jackson.databind.*;
...
@@ -26,12 +26,12 @@ import com.fasterxml.jackson.databind.*;
import
com.fasterxml.jackson.databind.type.CollectionLikeType
;
import
com.fasterxml.jackson.databind.type.CollectionLikeType
;
import
com.fasterxml.jackson.databind.type.MapType
;
import
com.fasterxml.jackson.databind.type.MapType
;
import
lombok.experimental.UtilityClass
;
import
lombok.experimental.UtilityClass
;
import
net.dreamlu.mica.core.format.FastDateFormat
;
import
net.dreamlu.mica.core.function.CheckedConsumer
;
import
net.dreamlu.mica.core.function.CheckedConsumer
;
import
org.springframework.lang.Nullable
;
import
org.springframework.lang.Nullable
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.text.SimpleDateFormat
;
import
java.time.ZoneId
;
import
java.time.ZoneId
;
import
java.util.*
;
import
java.util.*
;
...
@@ -564,7 +564,7 @@ public class JsonUtil {
...
@@ -564,7 +564,7 @@ public class JsonUtil {
JacksonObjectMapper
()
{
JacksonObjectMapper
()
{
super
(
jsonFactory
());
super
(
jsonFactory
());
super
.
setLocale
(
CHINA
);
super
.
setLocale
(
CHINA
);
super
.
setDateFormat
(
new
Simple
DateFormat
(
DateUtil
.
PATTERN_DATETIME
,
CHINA
));
super
.
setDateFormat
(
new
Fast
DateFormat
(
DateUtil
.
PATTERN_DATETIME
,
CHINA
));
// 单引号
// 单引号
super
.
configure
(
JsonParser
.
Feature
.
ALLOW_SINGLE_QUOTES
,
true
);
super
.
configure
(
JsonParser
.
Feature
.
ALLOW_SINGLE_QUOTES
,
true
);
// 忽略json字符串中不识别的属性
// 忽略json字符串中不识别的属性
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录