java获取统⼀时间格式_java统⼀处理⼤部分时间字符串为
Date格式
public static void main(String[] args) {
String a = "2016-11-30 17:16:00";
String b = "2015-07-13 11:11:22.02";
String c = "1394438072";
String d = "Jul 26, 2017 2:27:13 PM";
String e = "2019-03-31";
String g = "2019-04-01 03:13";
String h = "1584516482676";
String i = "2014-06-27T08:11:07.168Z";
String j = "Tue Jan 14 16:11:26 +0800 2020";
DateFormat formatTo = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = formatDate(a);
Date date2 = formatDate(b);
Date date3 = formatDate(c);
Date date4 = formatDate(d);
Date date5 = formatDate(e);
Date date6 = formatDate(g);
Date date7 = formatDate(h);
Date date8 = formatDate(i);
System.out.println(date!=null?formatTo.format(date):null);
System.out.println(date2!=null?formatTo.format(date2):null);
System.out.println(date3!=null?formatTo.format(date3):null);
System.out.println(date4!=null?formatTo.format(date4):null);
System.out.println(date5!=null?formatTo.format(date5):null);
System.out.println(date6!=null?formatTo.format(date6):null);
System.out.println(date7!=null?formatTo.format(date7):null);
error parse newSystem.out.println(date8!=null?formatTo.format(date8):null);
System.out.println();
}
private static Date formatDate(String str) {
SimpleDateFormat resultSdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
SimpleDateFormat resultSdfDate = new SimpleDateFormat("yyyy-MM-dd");
if (str == null) {
return null;
}
if (ains("CST")) {
try {
Date date = resultSdf.parse(str);
return date;
} catch (ParseException e) {
<("时间转换失败:{}", e.getMessage());
}
} else if (ains("Z")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'.'sss'Z'"); try {
Date date = sdf.parse(str);
return date;
} catch (ParseException e) {
<("时间转换失败:{}", e.getMessage());
}
} else if (ains("-") && ains(":") && str.length() == 16) {
try {
Date newDate = resultSdf.parse(str + ":00");
return newDate;
} catch (ParseException e) {
<("时间转换失败:{}", e.getMessage());
}
} else if (ains("-") && ains(":")) {
try {
Date newDate = resultSdf.parse(str);
return newDate;
} catch (ParseException e) {
<("时间转换失败:{}", e.getMessage());
}
} else if (ains("-") && !ains(":")) {
try {
Date newDate = resultSdfDate.parse(str);
return newDate;
} catch (ParseException e) {
<("时间转换失败:{}", e.getMessage());
}
} else if (ains(",") && (ains("AM") || ains("PM"))) {
try {
DateFormat formatFrom = new SimpleDateFormat("MMM dd,yyyy KK:mm:ss aa", Locale.ENGLISH); Date date = formatFrom.parse(str);
return date;
} catch (ParseException e) {
<("时间转换失败:{}", e.getMessage());
}
} else {
if (str.length() != 13) {
int decrease = 13 - str.length();
for (int i = 0; i < decrease; i++) {
str += "0";
}
}
try {
Date date = new Date(Long.parseLong(str));
return date;
} catch (NumberFormatException e) {
<("时间转换失败:{}", e.getMessage());
}
}
try {
Date date = new Date(str);
return date;
} catch (NumberFormatException e) {
log.info("时间转换失败:{}", e.getMessage());
}
return null;
}
借鉴于该博客:blog.csdn/zzq900503/article/details/38925225可以处理以下格式:
String a = "2016-11-30 17:16:00";
String b= "2015-07-13 11:11:22.02";
String c= "1394438072";
String d= "Jul 26, 2017 2:27:13 PM";
String e= "2019-03-31";
String g= "2019-04-01 03:13";
String h= "1584516482676";
String i= "2014-06-27T08:11:07.168Z";
String j= "Tue Jan 14 16:11:26 +0800 2020";
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论