13404089107
2026-04-20 f4b6843a1b3990453d2aef6d87b5ac30723c5870
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
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.tcm.training.admin.common;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
 
public class DateTimes {
    public static final String LOCAL_DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";
    public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";
    public static final String PATTERN = LOCAL_DATE_TIME_PATTERN;
 
    private static final DateTimeFormatter LOCAL_DATE_TIME_FORMATTER =
            DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_PATTERN);
    private static final DateTimeFormatter LOCAL_DATE_FORMATTER =
            DateTimeFormatter.ofPattern(LOCAL_DATE_PATTERN);
    private static final DateTimeFormatter LOCAL_TIME_FORMATTER =
            DateTimeFormatter.ofPattern(LOCAL_TIME_PATTERN);
 
    private DateTimes() {
    }
 
    public static LocalDateTime parse(String text) {
        if (text == null || text.trim().isEmpty()) {
            return null;
        }
        try {
            return LocalDateTime.parse(text, LOCAL_DATE_TIME_FORMATTER);
        } catch (Exception e) {
            throw new IllegalArgumentException("时间格式错误,需 " + LOCAL_DATE_TIME_PATTERN);
        }
    }
 
    public static String format(LocalDateTime dt) {
        if (dt == null) {
            return null;
        }
        return LOCAL_DATE_TIME_FORMATTER.format(dt);
    }
 
    public static String format(LocalDate date) {
        if (date == null) {
            return null;
        }
        return LOCAL_DATE_FORMATTER.format(date);
    }
 
    public static String format(LocalTime time) {
        if (time == null) {
            return null;
        }
        return LOCAL_TIME_FORMATTER.format(time);
    }
 
    public static DateTimeFormatter localDateTimeFormatter() {
        return LOCAL_DATE_TIME_FORMATTER;
    }
 
    public static DateTimeFormatter localDateFormatter() {
        return LOCAL_DATE_FORMATTER;
    }
 
    public static DateTimeFormatter localTimeFormatter() {
        return LOCAL_TIME_FORMATTER;
    }
}