日期:2014-05-20 浏览次数:20737 次
String str =fdsaf2006060743242 Pattern p = Pattern.compile("[1-2][0-9]{3}[0-1][0-9][0-3][0-9]?", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(ss); while (m.find()) { String s = m.group(); String y = s.substring(0, 4); String mon = s.substring(4, 6); String date = s.substring(6, 8); s = y + "-" + mon + "-" + date; } }
------解决方案--------------------
public static String formatDateStr(String src) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String ret = ""; try { Calendar calendar = Calendar.getInstance(); calendar.setTime(format.parse(src.trim())); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DAY_OF_MONTH); ret = year + "-" + (month < 10? "0" + month: month) + "-" + (day < 10? "0" + day: day); } catch(ParseException e) { e.printStackTrace(); } return ret; }
------解决方案--------------------