-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDates.java
More file actions
53 lines (43 loc) · 1.73 KB
/
Dates.java
File metadata and controls
53 lines (43 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.github.lokic.javaplus;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Dates {
private final static Map<String, DateTimeFormatter> DATE_FORMATTER_CACHE = new ConcurrentHashMap<>();
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
public static String format(LocalDate localDate, String format) {
if (localDate == null) {
return null;
}
return localDate.format(getDateTimeFormatter(format));
}
public static String format(LocalDateTime localDateTime, String format) {
if (localDateTime == null) {
return null;
}
return localDateTime.format(getDateTimeFormatter(format));
}
public static LocalDate parseLocalDate(String date, String format) {
return LocalDate.parse(date, getDateTimeFormatter(format));
}
public static LocalDateTime parseLocalDateTime(String date, String format) {
return LocalDateTime.parse(date, getDateTimeFormatter(format));
}
private static DateTimeFormatter getDateTimeFormatter(String format) {
return DATE_FORMATTER_CACHE.computeIfAbsent(format, DateTimeFormatter::ofPattern);
}
}