java中怎么把20140124103709转换成yyyy-MM-dd hh:mm:ss形式的日期啊?

2024-11-01 20:24:34
推荐回答(5个)
回答1:

    String sDateStr="20140124103709";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmss");
        try {
            System.out.println(sdf.parse(sDateStr));
        } catch (ParseException e) {
            //做转化失败的操作
        }
    }

应该这样就能满足你的要求了,不过要注意判断长度是否都是14位,以防转换出错

回答2:

给你一段代码,使用它就可以实现
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());
}
}

回答3:

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);
}

回答4:

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));

回答5:

Date date = sdf1.parse(); //yyyyMMddHHmmss
sdf2.format(date)////yyyy-MM-dd HH:mm:ss