String sDateStr="20140124103709";
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
try {
System.out.println(sdf.parse(sDateStr));
} catch (ParseException e) {
//做转化失败的操作
}
}
应该这样就能满足你的要求了,不过要注意判断长度是否都是14位,以防转换出错
给你一段代码,使用它就可以实现
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String dateString = "20140124103709";
try {
Date date = df.parse(dateString);
System.out.println(df.format(date));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
public String changeTime(String time,)throws Exception{
if(StringUtil.isNull(time)){
throw new Exception("The time must not be null or ''");
}
SimpleDateFormat oldFormat = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat newFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = oldFormat.parse(time);
return newFormat.format(date);
}
String s = "20140124103709";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = (Date) sdf1.parse(s);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.print(sdf2.format(date));
Date date = sdf1.parse(); //yyyyMMddHHmmss
sdf2.format(date)////yyyy-MM-dd HH:mm:ss