Skip to content

Commit 0ca3977

Browse files
committed
enhance Timestamp-LocalDateTime converter
1 parent bda38cb commit 0ca3977

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,6 @@ Solution
182182
- Adjustment:
183183
- Icons changed for align, friendly view etc.
184184
- The manual of this plugin has been moved to my <a href="http://www.debuggerpowerzcy.top/home/2020/03/14/D8gerAutoCode%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/">website</a>.
185-
186-
- 2.2(private)
187-
- Addition:
188-
- END easter eggs, try to type `end` in the blank file in IDEA to get the end-template(encryption and decryption).
189-
- END easter eggs, multi-lines can be batch handled with the end-template, it's very useful for encryption and decryption.
190-
- Optimization: Support linebreak or comma as the delimiter.
191-
- Attention: This version is private, you can <a href="http://www.debuggerpowerzcy.top/about/">send me a email</a> and clarify your needs, such as what algorithm you'll take.
192185

193186
- 2.1
194187
- BugFix: add Uncapitalize, and the circle is Camel-Underline-Uppercase-Uncapitalize-Lowercase.

README_CN.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,6 @@
180180
- 功能调整:
181181
- 插件功能图标调整对齐.
182182
- 插件使用指南搬迁到我的<a href="http://www.debuggerpowerzcy.top/home/2020/03/14/D8gerAutoCode%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%8C%87%E5%8D%97/">博客</a>.
183-
184-
- 2.2(private)
185-
- 新增功能:
186-
- 加解密彩蛋, 空白文档输入`end`获取加解密模板.
187-
- 加解密彩蛋, 针对多行文本, 提供多种加密算法下的加密、解密、加解密全过程的处理功能
188-
- 体验优化: 支持用中英文逗号作为多行文本的分隔符.
189-
- TheShy提示: 该版本是私有版本, 你可以向我<a href="http://www.debuggerpowerzcy.top/about/">发邮件</a>阐明你需要定制的加解密功能, 例如你要使用的加解密算法是哪一种.
190183

191184
- 2.1
192185
- Bug修复: 补充一键首字母小写转换功能, 完成字段命名闭环: 驼峰-下划线-全大写-首字母小写-全小写.

src/main/java/com/xyz/caofancpu/d8ger/action/CamelUnderlineConvertAction.java

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
import com.intellij.openapi.editor.SelectionModel;
1010
import com.intellij.openapi.project.Project;
1111
import com.intellij.openapi.util.TextRange;
12-
import com.xyz.caofancpu.d8ger.util.ConstantUtil;
1312
import com.xyz.caofancpu.d8ger.util.DateUtil;
1413
import com.xyz.caofancpu.d8ger.util.VerbalExpressionUtil;
1514
import lombok.NonNull;
1615
import org.apache.commons.lang3.StringUtils;
1716

18-
import java.util.regex.Pattern;
19-
2017
/**
2118
* 1.Camel-Underline convert
2219
* 2.Uppercase-Lowercase convert
@@ -25,9 +22,6 @@
2522
* @author caofanCPU
2623
*/
2724
public class CamelUnderlineConvertAction extends AnAction {
28-
29-
public static final Pattern DATE_TIME_REGEX = Pattern.compile("(?:[Tt])+");
30-
3125
@Override
3226
public void actionPerformed(AnActionEvent e) {
3327
final Editor currentEditor = e.getRequiredData(CommonDataKeys.EDITOR);
@@ -66,17 +60,12 @@ private void executeCamelUnderlineConvert(@NonNull Document currentDocument, @No
6660
private String handleReplacement(@NonNull String originWord) {
6761
String result = null;
6862
try {
69-
// First, 1600069557000 to 2020-09-14 15:45:57
63+
// First, 1600069557000 to 2020-09-14 15:45:57.000
7064
Long milliSeconds = Long.valueOf(originWord);
71-
result = DateUtil.toLocalDateTime(milliSeconds).toString().replaceAll(DATE_TIME_REGEX.pattern(), ConstantUtil.SPACE);
65+
result = DateUtil.enhanceToLocalDateTime(milliSeconds);
7266
} catch (Exception e) {
7367
// ignore
74-
try {
75-
// Second, 2020-09-14 15:45:57 to 1600069557000
76-
result = DateUtil.parseStandardMilliSeconds(originWord.replaceAll(DATE_TIME_REGEX.pattern(), ConstantUtil.SPACE)).toString();
77-
} catch (Exception exception) {
78-
// ignore
79-
}
68+
result = DateUtil.enhanceParseMilliSeconds(originWord);
8069
}
8170
if (StringUtils.isBlank(result)) {
8271
result = VerbalExpressionUtil.camelUnderLineNameConverter(originWord);

src/main/java/com/xyz/caofancpu/d8ger/util/DateUtil.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020

2121
import lombok.NonNull;
2222
import lombok.extern.slf4j.Slf4j;
23+
import org.apache.commons.lang3.StringUtils;
2324

2425
import java.time.Instant;
2526
import java.time.LocalDateTime;
2627
import java.time.ZoneOffset;
2728
import java.time.format.DateTimeFormatter;
29+
import java.util.Objects;
30+
import java.util.regex.Pattern;
2831

2932

3033
/**
@@ -34,7 +37,11 @@
3437
*/
3538
@Slf4j
3639
public class DateUtil {
40+
public static final Pattern DATE_TIME_REGEX = Pattern.compile("(?:[Tt])+");
3741
public final static String DATETIME_FORMAT_SIMPLE = "yyyy-MM-dd HH:mm:ss";
42+
public final static String DATETIME_FORMAT_DETAIL = "yyyy-MM-dd HH:mm:ss:SSS";
43+
public final static String DATETIME_FORMAT_DETAIL_DOT = "yyyy-MM-dd HH:mm:ss.SSS";
44+
public final static String DATETIME_FORMAT_CN = "yyyy年MM月dd日HH时mm分ss秒";
3845

3946
public final static ZoneOffset DEFAULT_ZONE_OFFSET = ZoneOffset.of("+8");
4047

@@ -65,4 +72,47 @@ public static LocalDateTime parseStandardDateTime(String dateTimeStr) {
6572
return LocalDateTime.parse(dateTimeStr, DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE));
6673
}
6774

75+
/**
76+
* Long to LocalDateTimeString
77+
*/
78+
public static String enhanceToLocalDateTime(@NonNull Long milliSeconds) {
79+
LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(milliSeconds), DEFAULT_ZONE_OFFSET);
80+
return localDateTime.format(DateTimeFormatter.ofPattern(DATETIME_FORMAT_DETAIL_DOT));
81+
}
82+
83+
/**
84+
* String of date and time, convert to String: milliseconds
85+
* for example:
86+
* 2020-09-14 15:45:57
87+
* 2020-09-14T15:45:57
88+
* 2020-09-14t15:45:57
89+
* 2020-09-14 15:45:57.007
90+
* 2020-09-14 15:45:57:007
91+
* 2020年09月14日15时45分57秒
92+
*
93+
* @param dateTimeStr
94+
* @return
95+
*/
96+
public static String enhanceParseMilliSeconds(String dateTimeStr) {
97+
if (StringUtils.isBlank(dateTimeStr)) {
98+
return dateTimeStr;
99+
}
100+
String originWord = dateTimeStr.replaceAll(DATE_TIME_REGEX.pattern(), ConstantUtil.SPACE);
101+
LocalDateTime parse = null;
102+
try {
103+
parse = LocalDateTime.parse(originWord, DateTimeFormatter.ofPattern(DATETIME_FORMAT_SIMPLE));
104+
} catch (Exception e) {
105+
try {
106+
parse = LocalDateTime.parse(originWord, DateTimeFormatter.ofPattern(DATETIME_FORMAT_DETAIL));
107+
} catch (Exception exception) {
108+
try {
109+
parse = LocalDateTime.parse(originWord, DateTimeFormatter.ofPattern(DATETIME_FORMAT_DETAIL_DOT));
110+
} catch (Exception ex) {
111+
parse = LocalDateTime.parse(originWord, DateTimeFormatter.ofPattern(DATETIME_FORMAT_CN));
112+
}
113+
}
114+
}
115+
return Objects.nonNull(parse) ? String.valueOf(parse.toInstant(DEFAULT_ZONE_OFFSET).toEpochMilli()) : dateTimeStr;
116+
}
117+
68118
}

src/main/resources/META-INF/plugin.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,6 @@
192192
</li>
193193
</ul>
194194
</br>
195-
<li><h1>2.2(private)</h1></li>
196-
<ul>
197-
<li>Addition:<br>
198-
<ul>
199-
<li>END easter eggs, try to type 'end' in the blank file in IDEA to get the end-template(encryption and decryption).</li>
200-
<li>END easter eggs, multi-lines can be batch handled with the end-template, it's very useful for encryption and decryption.</li>
201-
</ul>
202-
</li>
203-
<li>Optimization: Support linebreak or comma as the delimiter.</li>
204-
<li>Attention: This version is private, you can <a href="http://www.debuggerpowerzcy.top/about/">send me a email</a> and clarify your needs, such as what algorithm you'll take.</li>
205-
</ul>
206-
</br>
207195
<li><h1>2.1</h1></li>
208196
<ul>
209197
<li>BugFix: add Uncapitalize, and the circle is Camel-Underline-Uppercase-Uncapitalize-Lowercase.</li>

0 commit comments

Comments
 (0)