forked from Realhedin/topjava02
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimeUtil.java
More file actions
54 lines (42 loc) · 1.9 KB
/
TimeUtil.java
File metadata and controls
54 lines (42 loc) · 1.9 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
54
package ru.javawebinar.topjava.util;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* GKislin
* 07.01.2015.
*/
public class TimeUtil {
public static final DateTimeFormatter DATE_TME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm");
public static String toString(LocalDateTime ldt) {
return toString(ldt, DATE_TME_FORMATTER);
}
public static String toString(LocalDateTime ldt, DateTimeFormatter formatter) {
return ldt == null ? "" : ldt.format(formatter);
}
public static LocalDateTime toDateTime(String str) {
return toDateTime(str, DATE_TME_FORMATTER);
}
public static LocalDateTime toDateTime(String str, DateTimeFormatter formatter) {
return StringUtils.isEmpty(str) ? LocalDateTime.now() : LocalDateTime.parse(str, formatter);
}
public static LocalDate toDate(String str, LocalDate def) {
return StringUtils.isEmpty(str) ? def : LocalDate.parse(str, DATE_FORMATTER);
}
public static LocalTime toTime(String str, LocalTime def) {
return StringUtils.isEmpty(str) ? def : LocalTime.parse(str, TIME_FORMATTER);
}
public static LocalDateTime startDateTime(String str) {
return LocalDateTime.of(toDate(str, LocalDate.of(0, 1, 1)), LocalTime.MIN);
}
public static LocalDateTime endDateTime(String str) {
return LocalDateTime.of(toDate(str, LocalDate.of(3000, 1, 1)), LocalTime.MAX);
}
public static boolean isBetween(LocalTime lt, LocalTime startTime, LocalTime endTime) {
return lt.compareTo(startTime) >= 0 && lt.compareTo(endTime) <= 0;
}
}