Skip to content

Commit

Permalink
feat(util): Add common date-time formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Chan committed Jul 5, 2024
1 parent d86d2b8 commit bd619b7
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
68 changes: 68 additions & 0 deletions tile-util/src/main/java/com/power4j/tile/time/Fmt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.power4j.tile.time;

import lombok.experimental.UtilityClass;

import java.time.format.DateTimeFormatter;

/**
* Common date-time formatter.
*
* @author CJ ([email protected])
* @since 1.0
*/
@UtilityClass
public class Fmt {

/**
* Format: {@code yyyy-MM-dd HH:mm:ss}
*
* Example: <pre>
* LocalDateTime.parse("2020-01-01 12:00:00",Fmt.FMT_DATETIME)
* </pre>
*/
public static final DateTimeFormatter FMT_DATETIME = DateTimeFormatter.ofPattern(Patterns.DATETIME);

/**
* Format: {@code yyyy-MM-dd HH:mm:ss,SSS}
*/
public static final DateTimeFormatter FMT_DATETIME_ISO8601 = DateTimeFormatter.ofPattern(Patterns.DATETIME_ISO8601);

/**
* Format: {@code yyyyMMddHHmmss}
*/
public static final DateTimeFormatter FMT_DATETIME_PURE = DateTimeFormatter.ofPattern(Patterns.DATETIME_PURE);

/**
* Format: {@code yyyyMMddHHmmssSSS}
*/
public static final DateTimeFormatter FMT_DATETIME_MS_PURE = DateTimeFormatter.ofPattern(Patterns.DATETIME_MS_PURE);

/**
* The ISO date-time formatter that formats or parses a date-time with an offset
* <ul>
* <li>format output: {@code 2011-12-03T10:15:30+01:00} or
* {@code 2020-01-01T12:00:00Z}</li>
* <li>can parse input: {@code 2011-12-03T10:15:30+01:00} or
* {@code 2020-01-01T12:00:00Z} or {@code 20200101120000Z}</li>
* </ul>
* see <a href="https://zh.wikipedia.org/wiki/ISO_8601">ISO_8601</a>
*/
public static final DateTimeFormatter FMT_ISO_OFFSET_DATE_TIME = DateTimeFormatter.ISO_OFFSET_DATE_TIME;

}
4 changes: 1 addition & 3 deletions tile-util/src/main/java/com/power4j/tile/time/Patterns.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public interface Patterns {

String DATETIME_PURE = "yyyyMMddHHmmss";

String DATETIME_UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";

String DATETIME_MS_PURE = "yyyyMMddHHmmssSSS";

String DATETIME_UTC_ZONE = "yyyy-MM-dd'T'HH:mm:ssZ";
String DATETIME_ISO8601_UTC = "yyyy-MM-dd'T'HH:mm:ss'Z'";

String DATETIME_ISO8601 = "yyyy-MM-dd HH:mm:ss,SSS";

Expand Down
61 changes: 61 additions & 0 deletions tile-util/src/test/java/com/power4j/tile/time/FmtTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.power4j.tile.time;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
* @author CJ ([email protected])
* @since 1.0
*/
class FmtTest {

@Test
void localDateTimeTest() {
LocalDateTime dt = LocalDateTime.parse("2020-01-01 12:00:00", Fmt.FMT_DATETIME);
Assertions.assertEquals(LocalDateTime.of(2020, 1, 1, 12, 0, 0), dt);

dt = LocalDateTime.parse("2020-01-01 12:00:00,999", Fmt.FMT_DATETIME_ISO8601);
Assertions.assertEquals(LocalDateTime.of(2020, 1, 1, 12, 0, 0, 999_000_000), dt);

dt = LocalDateTime.parse("20200101120000", Fmt.FMT_DATETIME_PURE);
Assertions.assertEquals(LocalDateTime.of(2020, 1, 1, 12, 0, 0), dt);

dt = LocalDateTime.parse("20200101120000999", Fmt.FMT_DATETIME_MS_PURE);
Assertions.assertEquals(LocalDateTime.of(2020, 1, 1, 12, 0, 0, 999_000_000), dt);

}

@Test
void offsetDateTimeTest() {

OffsetDateTime odt = OffsetDateTime.parse("2020-01-01T12:00:00Z", Fmt.FMT_ISO_OFFSET_DATE_TIME);
Assertions.assertEquals(OffsetDateTime.of(2020, 1, 1, 12, 0, 0, 0, ZoneOffset.UTC), odt);
Assertions.assertEquals("2020-01-01T12:00:00Z", Fmt.FMT_ISO_OFFSET_DATE_TIME.format(odt));

odt = OffsetDateTime.parse("2011-12-03T10:15:30+01:00", Fmt.FMT_ISO_OFFSET_DATE_TIME);
Assertions.assertEquals(OffsetDateTime.of(2011, 12, 3, 10, 15, 30, 0, ZoneOffset.ofHours(1)), odt);
Assertions.assertEquals("2011-12-03T10:15:30+01:00", Fmt.FMT_ISO_OFFSET_DATE_TIME.format(odt));

}

}

0 comments on commit bd619b7

Please sign in to comment.