package timeutil; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class TimeTest { public static void main(String[] args) { String s1 = "20151117190936"; String s2="20090915-17:20:12"; try { Date date1 = new SimpleDateFormat("yyyyMMddHHmmss").parse(s1); //输出格式 System.out.println("=======解析字符串1======"); System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date1)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1)); System.out.println("=======解析字符串2======"); Date date2 = new SimpleDateFormat("yyyyMMdd-HH:mm:ss").parse(s2); System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date2)); System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date2)); } catch (ParseException e) { e.printStackTrace(); } } }
运行结果:
=======解析字符串1======
2015/11/17 19:09:36
2015-11-17 19:09:36
=======解析字符串2======
2009/09/15 17:20:12
2009-09-15 17:20:12