feat: 日期工具类新增方法
parent
4edcc0bca2
commit
da1b7b0351
|
|
@ -24,17 +24,29 @@ public final class DateTimes {
|
|||
}
|
||||
|
||||
public static String ofNow() {
|
||||
return DateTimeFormatter.ofPattern(NORMAL_DATE_TIME).format(LocalDateTime.now());
|
||||
return of(NORMAL_DATE_TIME);
|
||||
}
|
||||
|
||||
public static String ofPatternDate(Date date) {
|
||||
public static String ofNowDate() {
|
||||
return of(NORMAL_DATE);
|
||||
}
|
||||
|
||||
public static String ofTransDate() {
|
||||
return ofNowDate();
|
||||
}
|
||||
|
||||
public static String ofTransTradeTime() {
|
||||
return ofNow();
|
||||
}
|
||||
|
||||
public static String of(String pattern) {
|
||||
return DateTimeFormatter.ofPattern(pattern).format(LocalDateTime.now());
|
||||
}
|
||||
|
||||
public static String ofNormalPattern(Date date) {
|
||||
Objects.requireNonNull(date, "date is null");
|
||||
|
||||
LocalDateTime dateTime = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
return DateTimeFormatter.ofPattern(NORMAL_DATE_TIME).format(dateTime);
|
||||
}
|
||||
|
||||
public static String ofNowDate() {
|
||||
return DateTimeFormatter.ofPattern(NORMAL_DATE).format(LocalDateTime.now());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -108,8 +108,8 @@ public class Strings {
|
|||
}
|
||||
|
||||
public static String timeRange(X509Certificate certificate) {
|
||||
return String.format("%s,%s", DateTimes.ofPatternDate(certificate.getNotBefore()),
|
||||
DateTimes.ofPatternDate(certificate.getNotAfter()));
|
||||
return String.format("%s,%s", DateTimes.ofNormalPattern(certificate.getNotBefore()),
|
||||
DateTimes.ofNormalPattern(certificate.getNotAfter()));
|
||||
}
|
||||
|
||||
public static String toStr(Object data) {
|
||||
|
|
|
|||
|
|
@ -21,6 +21,13 @@ class DateTimesTest {
|
|||
Assertions.assertEquals(19, now.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
void of() {
|
||||
String now = DateTimes.of("yyyy-MM-dd");
|
||||
Assertions.assertNotNull(now);
|
||||
Assertions.assertEquals(10, now.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nowDate() {
|
||||
String nowDate = DateTimes.ofNowDate();
|
||||
|
|
@ -29,10 +36,10 @@ class DateTimesTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
void ofPatternDate() throws ParseException {
|
||||
void ofNormalPattern() throws ParseException {
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
String text = "2024-01-01 12:10:21";
|
||||
Assertions.assertEquals(text, DateTimes.ofPatternDate(format.parse(text)));
|
||||
Assertions.assertEquals(text, DateTimes.ofNormalPattern(format.parse(text)));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -40,4 +47,18 @@ class DateTimesTest {
|
|||
Assertions.assertEquals("yyyy-MM-dd HH:mm:ss", DateTimes.NORMAL_DATE_TIME);
|
||||
Assertions.assertEquals("yyyy-MM-dd", DateTimes.NORMAL_DATE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void ofTransDate() {
|
||||
String nowDate = DateTimes.ofNowDate();
|
||||
Assertions.assertNotNull(nowDate);
|
||||
Assertions.assertEquals(nowDate, DateTimes.ofTransDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ofTransTradeTime() {
|
||||
String now = DateTimes.ofNow();
|
||||
Assertions.assertNotNull(now);
|
||||
Assertions.assertEquals(now, DateTimes.ofTransTradeTime());
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue