public static void main(String[] args) {
String s = getFormatString("2019-06-24 23:12:03");
s = getFormatString("2019/06/24 23:12:03");
s = getFormatString("20190624 23:12:03");
s = getFormatString("2019-06-24");
s = getFormatString("2019/06/24");
s = getFormatString("20190624");
s = getFormatString("20sd0624");
System.out.println(s);
}
public static String getFormatString(String date) {
String result = "";
try {
if(date==null){
result= "";
}else if(date.contains("/") || date.contains("-")){
date = date.replaceAll("/", "-");
Date d = new SimpleDateFormat("yyyy-MM-dd").parse(date);
result = new SimpleDateFormat("yyyy-MM-dd").format(d);
}else{
Date d = new SimpleDateFormat("yyyyMMdd").parse(date);
result = new SimpleDateFormat("yyyy-MM-dd").format(d);
}
} catch (ParseException e) {
result= "";
e.printStackTrace();
}
return result;
}
java时间格式化整理
猜你喜欢
转载自blog.csdn.net/qiuzhi__ke/article/details/94740498
今日推荐
周排行