50,503
社区成员
发帖
与我相关
我的任务
分享
//这个数组中保存所有有可能的格式
SimpleDateFormat [] dateFormats= {new SimpleDateFormat("yyyy-MM-dd HH:mm"),new SimpleDateFormat("yyyy-MM-dd HH:mm")};
String times="2019-01-21 10:05";
for (SimpleDateFormat simpleDateFormat : dateFormats) {
try {
simpleDateFormat.parse(times);
times=simpleDateFormat.format(new Date());
break;
} catch (ParseException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
}
System.out.println(times);