格式化String的最快捷,最有效的方法

Bal*_*laB 1 java string string-matching

在Java中将日期是格式为"20110913"的字符串转换为"2011-09-13"的最快方法是什么?

duf*_*ymo 5

用途java.text.DateFormat:

DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
inputFormat.setLenient(false);
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
Date inputDate = inputFormat.parse("20110913);
System.out.println(outputFormat.format(inputDate));
Run Code Online (Sandbox Code Playgroud)