//format参数是日期,返回的是string,parse参数是string返回的是date
SimpleDateFormat sim1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sim3 = new SimpleDateFormat("yyyy年MM月dd日");
举个栗子
String s1="2022-12-16 12:12:12";
String s2="Tue Oct 18 00:00:00 CST 2022";
Date date1=timeDateFormart2(s1);
Date date2=timeDateFormart(s2);
System.out.println(date1);
System.out.println(date2);
//如果还要string就用format转
/**String =》date
* Tue Oct 18 00:00:00 CST 2022=>date
* **/
public static Date timeDateFormart(String time) {
SimpleDateFormat sim1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
SimpleDateFormat sim2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date dateStart = sim1.parse(time);
time=sim2.format(dateStart);
return sim2.parse(time);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**String =》date
* 2022-12-16 12:12:12=>date
* **/
public static Date timeDateFormart2(String time) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = null;
try {
parse = simpleDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}