java常用时间日志转换
# 常用时间转换
# Date -> LocalDate
public static LocalDate DateToLocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone).toLocalDate();
}
public static LocalDate DateToLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# LocalDate -> Date
public static Date LocalDateTodate(LocalDate) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
return Date.from(instant);
}
1
2
3
4
5
2
3
4
5
# Date -> LocalTime
public static LocalTime DateToLocalTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone).toLocalTime();
}
1
2
3
4
5
2
3
4
5
# LocalTime -> Date
public static Date LocalDateTimeToUdate(LocalTime localTime) {
ZoneId zone = ZoneId.systemDefault();
return Date.from(localDateTime.atZone(zone).toInstant());
}
1
2
3
4
2
3
4
# Date -> LocalDateTime
public static LocalDateTime DateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
return LocalDateTime.ofInstant(instant, zone);
}
public static LocalDateTime dateToLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# LocalDateTime -> Date
public static Date LocalTimeToDate(LocalDateTime localDateTime) {
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return Date.from(instant);
}
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# 常用日期Fomatter
/**
* HHmm
*/
public static final DateTimeFormatter FORMATTER_HHmm = DateTimeFormatter.ofPattern("HHmm");
/**
* yyyyMMdd
*/
public static final DateTimeFormatter FORMATTER_yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
/**
* yyyy-MM-dd
*/
public static final DateTimeFormatter FORMATTER_yyyyMMdd2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
* yyyyMMddHHmm
*/
public static final DateTimeFormatter FORMATTER_yyyyMMddHHmm = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
/**
* yyyy-MM-dd HHmm
*/
public static final DateTimeFormatter FORMATTER_yyyyMMddHHmm2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HHmm");
/**
* yyyy-MM-dd HH:mm
*/
public static final DateTimeFormatter FORMATTER_yyyyMMddHHmm3 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
/**
* yyyy-MM-dd HH:mm:ss
*/
public static final DateTimeFormatter FORMATTER_yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* yyMMdd
*/
public static final DateTimeFormatter FORMATTER_yyMMdd = DateTimeFormatter.ofPattern("yyMMdd");
/**
* ddMMMyy 支持大写的月份
*/
public static final DateTimeFormatter FORMATTER_ddMMMyy = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("ddMMMyy").toFormatter(Locale.ENGLISH);
/**
* ddMMM 支持大写的月份
*/
public static final DateTimeFormatter FORMATTER_ddMMM = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("ddMMM").toFormatter(Locale.ENGLISH);
/**
* ddMMMyyHHmm
*/
public static final DateTimeFormatter FORMATTER_ddMMMyyHHmm = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("ddMMMyyHHmm").toFormatter(Locale.ENGLISH);
/**
* yyMMMddHHmmss
*/
public static final DateTimeFormatter FORMATTER_yyMMMddHHmmss = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("yyMMMddHHmmss").toFormatter(Locale.ENGLISH);
/**
* yyMMddHHmmss
*/
public static final DateTimeFormatter FORMATTER_yyMMddHHmmss = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("yyMMddHHmmss").toFormatter(Locale.ENGLISH);
/**
* yyMMMdd
*/
public static final DateTimeFormatter FORMATTER_yyMMMdd = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("yyMMMdd").toFormatter(Locale.ENGLISH);
/**
* ddMMMyyyyHHmm
*/
public static final DateTimeFormatter FORMATTER_ddMMMyyyyHHmm = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("ddMMMyyyyHHmm").toFormatter(Locale.ENGLISH);
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
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
# String -> LocalDate
public static LocalDate stringToLocalDate(String date) {
return LocalDateTime.parse(time, FORMATTER_yyyyMMdd);
}
1
2
3
2
3
# String -> LocalTime
public static LocalTime stringToLocalTime(String date) {
return LocalDateTime.parse(time, FORMATTER_ddMMMyyHHmm);
}
1
2
3
2
3
# String -> LocalDateTime
public static LocalDateTime stringToLocalDateTime(String date) {
return LocalDateTime.parse(time, FORMATTER_ddMMMyyHHmm);
}
1
2
3
2
3
# LocalDate -> String
public static String localDateToString(LocalDate localDate) {
return FORMATTER_yyyyMMdd.format(localDate);
}
1
2
3
2
3
# LocalTime -> String
public static String localTimeToString(LocalTime localTime) {
return FORMATTER_yyyyMMdd.format(localTime);
}
1
2
3
2
3
# LocalDateTime -> String
public static String LocalDateTimeToString(LocalDateTime localDateTime ) {
return FORMATTER_ddMMMyyHHmm.format(localDateTime);
}
1
2
3
2
3