diff --git a/pom.xml b/pom.xml
index 16459ae5b..6ad3a2aba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.baidu.hugegraph
hugegraph-common
- 1.7.6
+ 1.7.7
hugegraph-common
https://github.com/hugegraph/hugegraph-common
@@ -260,7 +260,7 @@
- 1.7.6.0
+ 1.7.7.0
diff --git a/src/main/java/com/baidu/hugegraph/util/DateUtil.java b/src/main/java/com/baidu/hugegraph/util/DateUtil.java
new file mode 100644
index 000000000..71e89b5e7
--- /dev/null
+++ b/src/main/java/com/baidu/hugegraph/util/DateUtil.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2017 HugeGraph Authors
+ *
+ * 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 com.baidu.hugegraph.util;
+
+import java.text.ParseException;
+import java.util.Date;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import com.baidu.hugegraph.date.SafeDateFormat;
+import com.google.common.collect.ImmutableMap;
+
+public final class DateUtil {
+
+ public static final Date DATE_ZERO = new Date(0L);
+
+ private static final Map VALID_DFS = ImmutableMap.of(
+ "^\\d{4}-\\d{1,2}-\\d{1,2}",
+ "yyyy-MM-dd",
+ "^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}",
+ "yyyy-MM-dd HH:mm:ss",
+ "^\\d{4}-\\d{1,2}-\\d{1,2}\\s\\d{2}:\\d{2}:\\d{2}\\.\\d{1,3}",
+ "yyyy-MM-dd HH:mm:ss.SSS"
+ );
+
+ private static final Map DATE_FORMATS =
+ new ConcurrentHashMap<>();
+
+ public static Date parse(String value) {
+ for (Map.Entry entry : VALID_DFS.entrySet()) {
+ if (value.matches(entry.getKey())) {
+ try {
+ return parse(value, entry.getValue());
+ } catch (ParseException e) {
+ throw new IllegalArgumentException(String.format(
+ "%s, expect format: %s",
+ e.getMessage(), entry.getValue()));
+ }
+ }
+ }
+ throw new IllegalArgumentException(String.format(
+ "Expected date format is: %s, but got '%s'",
+ VALID_DFS.values(), value));
+ }
+
+ public static Date parse(String value, String df) throws ParseException {
+ SafeDateFormat dateFormat = getDateFormat(df);
+ return dateFormat.parse(value);
+ }
+
+ public static Date now() {
+ return new Date();
+ }
+
+ private static SafeDateFormat getDateFormat(String df) {
+ SafeDateFormat dateFormat = DATE_FORMATS.get(df);
+ if (dateFormat == null) {
+ dateFormat = new SafeDateFormat(df);
+ /*
+ * Specify whether or not date/time parsing is to be lenient.
+ * With lenient parsing, the parser may use heuristics to interpret
+ * inputs that do not precisely match this object's format.
+ * With strict parsing, inputs must match this object's format.
+ */
+ dateFormat.setLenient(false);
+ SafeDateFormat previous = DATE_FORMATS.putIfAbsent(df, dateFormat);
+ if (previous != null) {
+ dateFormat = previous;
+ }
+ }
+ return dateFormat;
+ }
+
+ public static Object toPattern(String df) {
+ SafeDateFormat dateFormat = getDateFormat(df);
+ return dateFormat.toPattern();
+ }
+}
diff --git a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java
index e1777ba6a..dc8d9c7ee 100644
--- a/src/main/java/com/baidu/hugegraph/version/CommonVersion.java
+++ b/src/main/java/com/baidu/hugegraph/version/CommonVersion.java
@@ -27,5 +27,5 @@ public class CommonVersion {
// The second parameter of Version.of() is for all-in-one JAR
public static final Version VERSION = Version.of(CommonVersion.class,
- "1.7.6");
+ "1.7.7");
}
diff --git a/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java b/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java
index dc90b35bf..d0d651be1 100644
--- a/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java
+++ b/src/test/java/com/baidu/hugegraph/unit/UnitTestSuite.java
@@ -47,6 +47,7 @@
import com.baidu.hugegraph.unit.rest.RestResultTest;
import com.baidu.hugegraph.unit.util.BytesTest;
import com.baidu.hugegraph.unit.util.CollectionUtilTest;
+import com.baidu.hugegraph.unit.util.DateUtilTest;
import com.baidu.hugegraph.unit.util.HashUtilTest;
import com.baidu.hugegraph.unit.util.InsertionOrderUtilTest;
import com.baidu.hugegraph.unit.util.LongEncodingTest;
@@ -57,6 +58,7 @@
import com.baidu.hugegraph.unit.util.TimeUtilTest;
import com.baidu.hugegraph.unit.util.VersionUtilTest;
import com.baidu.hugegraph.unit.version.VersionTest;
+import com.baidu.hugegraph.util.DateUtil;
@RunWith(Suite.class)
@Suite.SuiteClasses({
@@ -90,6 +92,7 @@
VersionUtilTest.class,
LongEncodingTest.class,
OrderLimitMapTest.class,
+ DateUtilTest.class,
ExtraParamTest.class,
LicenseCreateParamTest.class,
diff --git a/src/test/java/com/baidu/hugegraph/unit/util/DateUtilTest.java b/src/test/java/com/baidu/hugegraph/unit/util/DateUtilTest.java
new file mode 100644
index 000000000..b84384e07
--- /dev/null
+++ b/src/test/java/com/baidu/hugegraph/unit/util/DateUtilTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2017 HugeGraph Authors
+ *
+ * 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 com.baidu.hugegraph.unit.util;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+import com.baidu.hugegraph.testutil.Assert;
+import com.baidu.hugegraph.unit.BaseUnitTest;
+import com.baidu.hugegraph.util.DateUtil;
+
+public class DateUtilTest extends BaseUnitTest {
+
+ @Test
+ public void testParse() {
+ Date date1 = DateUtil.parse("2020-06-12 12:00:00");
+ Date date2 = DateUtil.parse("2020-06-13");
+ Assert.assertTrue(date1.before(date2));
+
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ DateUtil.parse("2018-");
+ }, e -> {
+ Assert.assertContains("Expected date format is:",
+ e.getMessage());
+ });
+
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ DateUtil.parse("2018-15-07 12:00:00");
+ }, e -> {
+ Assert.assertContains(", expect format: ", e.getMessage());
+ });
+ }
+
+ @Test
+ public void testNow() {
+ Date date1 = DateUtil.now();
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ // ignore
+ }
+ Date date2 = DateUtil.now();
+ Assert.assertTrue(date1.before(date2));
+ }
+
+ @Test
+ public void testToPattern() {
+ Object pattern = DateUtil.toPattern("yyyyMMdd HH:mm:ss.SSS");
+ Assert.assertEquals("yyyyMMdd HH:mm:ss.SSS", pattern);
+
+ Assert.assertThrows(IllegalArgumentException.class, () -> {
+ DateUtil.toPattern("iyyyyMMdd");
+ }, e -> {
+ Assert.assertContains("Illegal pattern character 'i'", e.getMessage());
+ });
+ }
+}
diff --git a/src/test/java/com/baidu/hugegraph/unit/util/ReflectionUtilTest.java b/src/test/java/com/baidu/hugegraph/unit/util/ReflectionUtilTest.java
index 79f365a1d..7b402daf1 100644
--- a/src/test/java/com/baidu/hugegraph/unit/util/ReflectionUtilTest.java
+++ b/src/test/java/com/baidu/hugegraph/unit/util/ReflectionUtilTest.java
@@ -91,7 +91,7 @@ public void testClasses() throws IOException {
@SuppressWarnings("unchecked")
List classes = IteratorUtils.toList(ReflectionUtil.classes(
"com.baidu.hugegraph.util"));
- Assert.assertEquals(15, classes.size());
+ Assert.assertEquals(16, classes.size());
classes.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
Assert.assertEquals("com.baidu.hugegraph.util.Bytes",
classes.get(0).getName());
@@ -100,7 +100,7 @@ public void testClasses() throws IOException {
Assert.assertEquals("com.baidu.hugegraph.util.CollectionUtil",
classes.get(2).getName());
Assert.assertEquals("com.baidu.hugegraph.util.VersionUtil",
- classes.get(14).getName());
+ classes.get(15).getName());
}
@Test