-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
micro-common/src/main/java/com/jiangtj/platform/common/DatetimeFormatters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
package com.jiangtj.platform.common; | ||
|
||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeFormatterBuilder; | ||
|
||
public interface DatetimeFormatters { | ||
DateTimeFormatter DateTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); | ||
DateTimeFormatter Date = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
DateTimeFormatter Time = DateTimeFormatter.ofPattern("HH:mm:ss"); | ||
// RFC3339 for wechat pay v3 | ||
DateTimeFormatter RFC3339 = new DateTimeFormatterBuilder() | ||
.parseCaseInsensitive() | ||
.append(Date) | ||
.appendLiteral("T") | ||
.append(Time) | ||
.parseLenient() | ||
.appendOffsetId() | ||
.parseStrict() | ||
.toFormatter(); | ||
} |
29 changes: 29 additions & 0 deletions
29
micro-common/src/test/java/com/jiangtj/platform/common/DatetimeFormattersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.jiangtj.platform.common; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class DatetimeFormattersTest { | ||
|
||
@Test | ||
public void testFormatters() { | ||
LocalDate date = LocalDate.of(2024, 2, 3); | ||
LocalTime time = LocalTime.of(8, 30, 40); | ||
LocalDateTime datetime = LocalDateTime.of(date, time); | ||
ZonedDateTime zonedDatetime = ZonedDateTime.of(datetime, ZoneId.of("Asia/Shanghai")); | ||
|
||
assertEquals("2024-02-03", date.format(DatetimeFormatters.Date)); | ||
assertEquals("2024-02-03", datetime.format(DatetimeFormatters.Date)); | ||
assertEquals("2024-02-03", zonedDatetime.format(DatetimeFormatters.Date)); | ||
assertEquals("08:30:40", time.format(DatetimeFormatters.Time)); | ||
assertEquals("08:30:40", datetime.format(DatetimeFormatters.Time)); | ||
assertEquals("08:30:40", zonedDatetime.format(DatetimeFormatters.Time)); | ||
assertEquals("2024-02-03 08:30:40", datetime.format(DatetimeFormatters.DateTime)); | ||
assertEquals("2024-02-03 08:30:40", zonedDatetime.format(DatetimeFormatters.DateTime)); | ||
assertEquals("2024-02-03T08:30:40+08:00", zonedDatetime.format(DatetimeFormatters.RFC3339)); | ||
} | ||
|
||
} |