Skip to content

Commit

Permalink
Add DateUtils UT
Browse files Browse the repository at this point in the history
  • Loading branch information
EricJoy2048 committed Apr 7, 2024
1 parent 2a97ec0 commit 4046531
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public class DateUtils {
* @return the DateTimeFormatter matched, will return null when not matched any pattern in
* {@link #PATTERN_ARRAY}
*/
public static DateTimeFormatter matchDateTimeFormatter(String dateTime) {
public static DateTimeFormatter matchDateFormatter(String dateTime) {
for (int j = 0; j < PATTERN_ARRAY.length; j++) {
if (PATTERN_ARRAY[j].matcher(dateTime).matches()) {
return DATE_FORMATTER_MAP.get(PATTERN_ARRAY[j]);
Expand All @@ -134,6 +134,11 @@ public static DateTimeFormatter matchDateTimeFormatter(String dateTime) {
return null;
}

public static LocalDate parse(String date) {
DateTimeFormatter dateTimeFormatter = matchDateFormatter(date);
return parse(date, dateTimeFormatter);
}

public static LocalDate parse(String date, DateTimeFormatter dateTimeFormatter) {
return LocalDate.parse(date, dateTimeFormatter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public void testPerformance() {
DateTimeUtils.parse(datetimeStr1);
}
long t6 = System.currentTimeMillis();
// If format is not specified, the system automatically obtains the format 'yyyy-MM-dd
// HH:mm:ss' for processing, use time: 4842ms
// If format is not specified, the system automatically obtains the format 'yyyyMMddHHmmss'
// for processing, use time: 4842ms

System.out.println((t6 - t5) + "");

Expand All @@ -194,14 +194,14 @@ public void testPerformance() {
DateTimeUtils.parse(datetimeStr2, dateTimeFormatter2);
}
long t8 = System.currentTimeMillis();
// Use an explicit time format 'yyyy-MM-dd HH:mm:ss.SSS' for processing, use time: 8162ms
// Use an explicit time format 'yyyy.MM.dd HH:mm:ss.SSS' for processing, use time: 8162ms
System.out.println((t8 - t7) + "");

for (int i = 0; i < 10000000; i++) {
DateTimeUtils.parse(datetimeStr2);
}
long t9 = System.currentTimeMillis();
// If format is not specified, the system automatically obtains the format 'yyyy-MM-dd
// If format is not specified, the system automatically obtains the format 'yyyy.MM.dd
// HH:mm:ss.SSS' for processing, use time: 11366ms
System.out.println((t9 - t8) + "");

Expand All @@ -210,14 +210,14 @@ public void testPerformance() {
DateTimeUtils.parse(datetimeStr3, dateTimeFormatter3);
}
long t11 = System.currentTimeMillis();
// Use an explicit time format 'yyyy-MM-dd HH:mm:ss' for processing, use time: 4405ms
// Use an explicit time format 'yyyy.MM.dd HH:mm:ss' for processing, use time: 4405ms
System.out.println((t11 - t10) + "");

for (int i = 0; i < 10000000; i++) {
DateTimeUtils.parse(datetimeStr3);
}
long t12 = System.currentTimeMillis();
// If format is not specified, the system automatically obtains the format 'yyyy-MM-dd
// If format is not specified, the system automatically obtains the format 'yyyy.MM.dd
// HH:mm:ss' for processing, use time: 7771ms
System.out.println((t12 - t11) + "");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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 org.apache.seatunnel.common.utils;

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

public class DateUtilsTest {

@Test
public void testAutoDateFormatter() {
String datetimeStr = "2020-10-10";
Assertions.assertEquals("2020-10-10", DateUtils.parse(datetimeStr).toString());

datetimeStr = "2020年10月10日";
Assertions.assertEquals("2020-10-10", DateUtils.parse(datetimeStr).toString());

datetimeStr = "2020/10/10";
Assertions.assertEquals("2020-10-10", DateUtils.parse(datetimeStr).toString());

datetimeStr = "2020.10.10";
Assertions.assertEquals("2020-10-10", DateUtils.parse(datetimeStr).toString());

datetimeStr = "20201010";
Assertions.assertEquals("2020-10-10", DateUtils.parse(datetimeStr).toString());
}

@Test
public void testMatchDateTimeFormatter() {
String datetimeStr = "2020-10-10";
Assertions.assertEquals(
"2020-10-10",
DateUtils.parse(datetimeStr, DateUtils.matchDateFormatter(datetimeStr)).toString());

datetimeStr = "2020年10月10日";
Assertions.assertEquals(
"2020-10-10",
DateUtils.parse(datetimeStr, DateUtils.matchDateFormatter(datetimeStr)).toString());

datetimeStr = "2020/10/10";
Assertions.assertEquals(
"2020-10-10",
DateUtils.parse(datetimeStr, DateUtils.matchDateFormatter(datetimeStr)).toString());

datetimeStr = "2020.10.10";
Assertions.assertEquals(
"2020-10-10",
DateUtils.parse(datetimeStr, DateUtils.matchDateFormatter(datetimeStr)).toString());

datetimeStr = "20201010";
Assertions.assertEquals(
"2020-10-10",
DateUtils.parse(datetimeStr, DateUtils.matchDateFormatter(datetimeStr)).toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ private LocalDate convertToLocalDate(JsonNode jsonNode, String fieldName) {
String dateStr = jsonNode.asText();
DateTimeFormatter dateFormatter = fieldFormatterMap.get(fieldName);
if (dateFormatter == null) {
dateFormatter = DateUtils.matchDateTimeFormatter(dateStr);
dateFormatter = DateUtils.matchDateFormatter(dateStr);
fieldFormatterMap.put(fieldName, dateFormatter);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Object convert(
case DATE:
DateTimeFormatter dateFormatter = fieldFormatterMap.get(fieldName);
if (dateFormatter == null) {
dateFormatter = DateUtils.matchDateTimeFormatter(field);
dateFormatter = DateUtils.matchDateFormatter(field);
fieldFormatterMap.put(fieldName, dateFormatter);
}

Expand Down

0 comments on commit 4046531

Please sign in to comment.