Skip to content

Commit 77ee578

Browse files
committed
add date utils
1 parent 87af34e commit 77ee578

4 files changed

Lines changed: 213 additions & 108 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.ss.rlib.common.util;
2+
3+
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
4+
import org.jetbrains.annotations.NotNull;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
import java.time.Instant;
8+
import java.time.LocalDate;
9+
import java.time.LocalDateTime;
10+
import java.time.ZoneOffset;
11+
import java.time.format.DateTimeFormatter;
12+
import java.time.temporal.TemporalAccessor;
13+
14+
/**
15+
* The class with utility methods to work with dates and times.
16+
*
17+
* @author JavaSaBr
18+
*/
19+
public class DateUtils {
20+
21+
private static final DateTimeFormatter TIMESTAMP_FORMATTER =
22+
DateTimeFormatter.ofPattern("HH:mm:ss:SSS");
23+
24+
/**
25+
* Format a time to a string by pattern HH:mm:ss:SSS
26+
*
27+
* @param timestamp the timestamp.
28+
* @return the string presentation.
29+
* @since 9.3.0
30+
*/
31+
public static @NotNull String formatShortTimestamp(long timestamp) {
32+
return TIMESTAMP_FORMATTER.format(LocalDateTime.ofInstant(
33+
Instant.ofEpochMilli(timestamp),
34+
ZoneOffset.UTC
35+
));
36+
}
37+
38+
/**
39+
* Format some temporal accessor to a string by pattern HH:mm:ss:SSS
40+
*
41+
* @param temporal the timestamp.
42+
* @return the string presentation.
43+
* @since 9.3.0
44+
*/
45+
public static @NotNull String formatShortTimestamp(@NotNull TemporalAccessor temporal) {
46+
return TIMESTAMP_FORMATTER.format(temporal);
47+
}
48+
49+
/**
50+
* Convert a date string to a {@link LocalDate}.
51+
*
52+
* @param string the string to convert.
53+
* @return the local date or null if this string cannot be converted.
54+
* @since 9.3.0
55+
*/
56+
public static @Nullable LocalDate toLocalDate(@Nullable String string) {
57+
if (StringUtils.isEmpty(string)) {
58+
return null;
59+
} else {
60+
return Utils.tryGetAndConvert(string, ISO_LOCAL_DATE::parse, LocalDate::from);
61+
}
62+
}
63+
64+
/**
65+
* Convert a local date to a string by ISO_LOCAL_DATE formatter.
66+
*
67+
* @param localDate the local date.
68+
* @return the string presentation of the local date or null.
69+
* @since 9.3.0
70+
*/
71+
public static @Nullable String toString(@Nullable LocalDate localDate) {
72+
if (localDate == null) {
73+
return null;
74+
} else {
75+
return ISO_LOCAL_DATE.format(localDate);
76+
}
77+
}
78+
79+
/**
80+
* Convert a temporal accessor to a string by passed formatter.
81+
*
82+
* @param temporalAccessor the temporal accessor.
83+
* @param formatter the formatter.
84+
* @return the string presentation or null.
85+
* @since 9.3.0
86+
*/
87+
public static @Nullable String toString(
88+
@Nullable TemporalAccessor temporalAccessor,
89+
@NotNull DateTimeFormatter formatter
90+
) {
91+
if (temporalAccessor == null) {
92+
return null;
93+
} else {
94+
return formatter.format(temporalAccessor);
95+
}
96+
}
97+
}

rlib-common/src/main/java/com/ss/rlib/common/util/StringUtils.java

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.ss.rlib.common.util;
22

3-
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
43
import org.jetbrains.annotations.NotNull;
54
import org.jetbrains.annotations.Nullable;
65

@@ -9,9 +8,6 @@
98
import java.math.BigInteger;
109
import java.security.MessageDigest;
1110
import java.security.NoSuchAlgorithmException;
12-
import java.time.*;
13-
import java.time.format.DateTimeFormatter;
14-
import java.time.temporal.TemporalAccessor;
1511
import java.util.concurrent.ThreadLocalRandom;
1612
import java.util.regex.Pattern;
1713

@@ -33,9 +29,6 @@ public class StringUtils {
3329
private static final ThreadLocal<MessageDigest> LOCAL_HASH_MD =
3430
ThreadLocal.withInitial(StringUtils::getHashMD5);
3531

36-
private static final DateTimeFormatter TIMESTAMP_FORMETTER =
37-
DateTimeFormatter.ofPattern("HH:mm:ss:SSS");
38-
3932
/**
4033
* Return an empty string if the received string is null.
4134
*
@@ -57,46 +50,6 @@ public class StringUtils {
5750
return isEmpty(string) ? another : string;
5851
}
5952

60-
/**
61-
* Format a time to a string by pattern HH:mm:ss:SSS
62-
*
63-
* @param timestamp the timestamp.
64-
* @return the string presentation.
65-
* @since 9.3.0
66-
*/
67-
public static @NotNull String formatShortTimestamp(long timestamp) {
68-
return TIMESTAMP_FORMETTER.format(LocalDateTime.ofInstant(
69-
Instant.ofEpochMilli(timestamp),
70-
ZoneOffset.UTC
71-
));
72-
}
73-
74-
/**
75-
* Format some temporal accessor to a string by pattern HH:mm:ss:SSS
76-
*
77-
* @param temporal the timestamp.
78-
* @return the string presentation.
79-
* @since 9.3.0
80-
*/
81-
public static @NotNull String formatShortTimestamp(@NotNull TemporalAccessor temporal) {
82-
return TIMESTAMP_FORMETTER.format(temporal);
83-
}
84-
85-
/**
86-
* Convert a date string to a {@link LocalDate}.
87-
*
88-
* @param string the string to convert.
89-
* @return the local date or null if this string cannot be converted.
90-
* @since 9.3.0
91-
*/
92-
public static @Nullable LocalDate toLocalDate(@Nullable String string) {
93-
if (StringUtils.isEmpty(string)) {
94-
return null;
95-
} else {
96-
return Utils.tryGetAndConvert(string, ISO_LOCAL_DATE::parse, LocalDate::from);
97-
}
98-
}
99-
10053
/**
10154
* Check a string email.
10255
*
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package com.ss.rlib.common.test.util;
2+
3+
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE;
4+
import static java.time.format.DateTimeFormatter.ISO_LOCAL_DATE_TIME;
5+
import com.ss.rlib.common.util.DateUtils;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import java.time.LocalDate;
10+
import java.time.LocalDateTime;
11+
import java.time.ZoneOffset;
12+
import java.time.ZonedDateTime;
13+
14+
public class DateUtilsTests {
15+
16+
@Test
17+
void stringToLocalDateTest() {
18+
19+
Assertions.assertEquals(
20+
LocalDate.of(1800, 5, 20),
21+
DateUtils.toLocalDate("1800-05-20")
22+
);
23+
24+
Assertions.assertNull(DateUtils.toLocalDate("2020-1-10"));
25+
Assertions.assertEquals(
26+
LocalDate.of(2020, 1, 10),
27+
DateUtils.toLocalDate("2020-01-10")
28+
);
29+
30+
Assertions.assertNull(DateUtils.toLocalDate("2015-5-1"));
31+
Assertions.assertEquals(
32+
LocalDate.of(2015, 5, 1),
33+
DateUtils.toLocalDate("2015-05-01")
34+
);
35+
36+
Assertions.assertNull(DateUtils.toLocalDate("invaliddate"));
37+
}
38+
39+
@Test
40+
void localDateToStringTest() {
41+
42+
Assertions.assertEquals(
43+
"1800-05-20",
44+
DateUtils.toString(LocalDate.of(1800, 5, 20))
45+
);
46+
47+
Assertions.assertEquals(
48+
"2020-01-10",
49+
DateUtils.toString(LocalDate.of(2020, 1, 10))
50+
);
51+
52+
Assertions.assertEquals(
53+
"2015-05-01",
54+
DateUtils.toString(LocalDate.of(2015, 5, 1))
55+
);
56+
57+
Assertions.assertNull(DateUtils.toString(null));
58+
}
59+
60+
@Test
61+
void temporalAccessorToStringTest() {
62+
63+
Assertions.assertEquals(
64+
"1800-05-20",
65+
DateUtils.toString(LocalDate.of(1800, 5, 20), ISO_LOCAL_DATE)
66+
);
67+
68+
Assertions.assertEquals(
69+
"2020-01-10T23:42:00",
70+
DateUtils.toString(
71+
LocalDateTime.of(2020, 1, 10, 23, 42),
72+
ISO_LOCAL_DATE_TIME
73+
)
74+
);
75+
76+
Assertions.assertNull(DateUtils.toString(null, ISO_LOCAL_DATE));
77+
}
78+
79+
@Test
80+
void formatTimestampTest() {
81+
82+
var localDateTime = LocalDateTime.of(
83+
2010,
84+
5,
85+
12,
86+
23,
87+
10,
88+
35,
89+
0
90+
);
91+
92+
Assertions.assertEquals("23:10:35:000", DateUtils.formatShortTimestamp(localDateTime));
93+
Assertions.assertEquals("23:10:35:000", DateUtils.formatShortTimestamp(localDateTime
94+
.toInstant(ZoneOffset.UTC)
95+
.toEpochMilli())
96+
);
97+
98+
var zonedDateTime = ZonedDateTime.of(
99+
2010,
100+
5,
101+
12,
102+
23,
103+
10,
104+
35,
105+
0,
106+
ZoneOffset.ofHours(3)
107+
);
108+
109+
Assertions.assertEquals("23:10:35:000", DateUtils.formatShortTimestamp(zonedDateTime));
110+
Assertions.assertEquals("23:10:35:000", DateUtils.formatShortTimestamp(zonedDateTime
111+
.toLocalDateTime()
112+
.toInstant(ZoneOffset.UTC)
113+
.toEpochMilli())
114+
);
115+
}
116+
}

rlib-common/src/test/java/com/ss/rlib/common/test/util/StringUtilsTest.java

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -120,65 +120,4 @@ void shouldDetectEmails() {
120120
Assertions.assertFalse(StringUtils.isEmail("test@test"));
121121
Assertions.assertFalse(StringUtils.isEmail("test@test."));
122122
}
123-
124-
@Test
125-
void convertToLocalDateTest() {
126-
127-
Assertions.assertEquals(
128-
LocalDate.of(1800, 5, 20),
129-
StringUtils.toLocalDate("1800-05-20")
130-
);
131-
132-
Assertions.assertNull(StringUtils.toLocalDate("2020-1-10"));
133-
Assertions.assertEquals(
134-
LocalDate.of(2020, 1, 10),
135-
StringUtils.toLocalDate("2020-01-10")
136-
);
137-
138-
Assertions.assertNull(StringUtils.toLocalDate("2015-5-1"));
139-
Assertions.assertEquals(
140-
LocalDate.of(2015, 5, 1),
141-
StringUtils.toLocalDate("2015-05-01")
142-
);
143-
144-
Assertions.assertNull(StringUtils.toLocalDate("invaliddate"));
145-
}
146-
147-
@Test
148-
void formatTimestampTest() {
149-
150-
var localDateTime = LocalDateTime.of(
151-
2010,
152-
5,
153-
12,
154-
23,
155-
10,
156-
35,
157-
0
158-
);
159-
160-
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(localDateTime));
161-
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(localDateTime
162-
.toInstant(ZoneOffset.UTC)
163-
.toEpochMilli())
164-
);
165-
166-
var zonedDateTime = ZonedDateTime.of(
167-
2010,
168-
5,
169-
12,
170-
23,
171-
10,
172-
35,
173-
0,
174-
ZoneOffset.ofHours(3)
175-
);
176-
177-
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(zonedDateTime));
178-
Assertions.assertEquals("23:10:35:000", StringUtils.formatShortTimestamp(zonedDateTime
179-
.toLocalDateTime()
180-
.toInstant(ZoneOffset.UTC)
181-
.toEpochMilli())
182-
);
183-
}
184123
}

0 commit comments

Comments
 (0)