String 转 Date
public Date string2Date(String date){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try{
return format.parse(date);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
13位时间戳 转 String
public String timeStamp2String(String timeStamp,String format) {
if(timeStamp == null || timeStamp.isEmpty() || timeStamp.equals("null")){
return "";
}
if(format == null || format.isEmpty()){
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(timeStamp)));
}
10位时间戳 转 String
public String timeStamp2String(String timeStamp,String format) {
if(timeStamp == null || timeStamp.isEmpty() || timeStamp.equals("null")){
return "";
}
if(format == null || format.isEmpty()){
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(timeStamp+"000")));
}
Date 转 LocalDate
public LocalDate date2LocalDate(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
return localDateTime.toLocalDate();
}
时间戳 转 LocalDate
public LocalDate timeStamp2LocalDate(String timeStamp,String format){
return date2LocalDate(string2Date(timeStamp2String(timeStamp,format)));
}
网友评论