/** * 把 Jul 8, 2020 12:00:00 AM 格式的时间转换为 2020-07-08 12:00:00 格式的时间 * * @param date Jul 8, 2020 12:00:00 AM 格式的时间 * @param type true 返回年月日格式 false 返回年月日时分秒 */ public static String parse_date(String date, boolean type) { SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH); Date d2 = null; try { //把Jul 8, 2020 12:00:00 AM格式转换为常规的Date格式 d2 = sdf.parse(date); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat simpleDateFormat; if (type) { simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); } else { simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); } //再把Date的时间转换为需要的格式 String format = simpleDateFormat.format(d2); return format; } /** * 把 2020-07-08 12:00:00 格式的时间转换为 Jul 8, 2020 12:00:00 AM 格式的时间 * * @param date 年月日时分秒 格式的时间 */ public static String format_date(String date) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d2 = null; try { //把 2020-07-08 12:00:00 格式的时间转换为 常规Date时间 d2 = simpleDateFormat.parse(date); } catch (ParseException e) { e.printStackTrace(); } SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy K:m:s a", Locale.ENGLISH); //再把 常规Date时间转换回 Jul 8, 2020 12:00:00 AM 格式的时间 String format = sdf.format(d2); return format; }