DateUtil.java
1.2 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
package com.tianbo.util.Date;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
public final class DateUtil {
private static Date currentDate = new Date();
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static SimpleDateFormat timesdf = new SimpleDateFormat("yyyyMMddHHmmss");
public static String getToday(){
return sdf.format(currentDate);
}
public static String getDDTM(){
return timesdf.format(currentDate);
}
public static Date formatByyyyyMMddHHmmss(String dateStr) throws DateTimeParseException{
//毫秒级的去掉
if(dateStr.length()>14){
dateStr= dateStr.substring(0,14);
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter);
return convertLDTToDate(dateTime);
}
//LocalDateTime转换为Date
public static Date convertLDTToDate(LocalDateTime time) {
return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
}
}