forked from knastnt/JavaRush-Training-Topjava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDateTimeUtil.java
More file actions
42 lines (32 loc) · 1.5 KB
/
DateTimeUtil.java
File metadata and controls
42 lines (32 loc) · 1.5 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
package ru.javawebinar.topjava.util;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class DateTimeUtil {
public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm";
public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern(DATE_TIME_PATTERN);
// DB doesn't support LocalDate.MIN/MAX
private static final LocalDateTime MIN_DATE = LocalDateTime.of(1, 1, 1, 0, 0);
private static final LocalDateTime MAX_DATE = LocalDateTime.of(3000, 1, 1, 0, 0);
private DateTimeUtil() {
}
public static LocalDateTime atStartOfDayOrMin(LocalDate localDate) {
return localDate != null ? localDate.atStartOfDay() : MIN_DATE;
}
public static LocalDateTime atStartOfNextDayOrMax(LocalDate localDate) {
return localDate != null ? localDate.plus(1, ChronoUnit.DAYS).atStartOfDay() : MAX_DATE;
}
public static String toString(LocalDateTime ldt) {
return ldt == null ? "" : ldt.format(DATE_TIME_FORMATTER);
}
public static @Nullable LocalDate parseLocalDate(@Nullable String str) {
return StringUtils.isEmpty(str) ? null : LocalDate.parse(str);
}
public static @Nullable LocalTime parseLocalTime(@Nullable String str) {
return StringUtils.isEmpty(str) ? null : LocalTime.parse(str);
}
}