欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java,

来源: javaer 分享于  点击 11343 次 点评:134

java,


1.问:怎么将“20090909090909”转换成“2009-09-09 09:09:09”

方法:public String getTime(String dateString)
{
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
try {
//将不规则字符串转换为日期
Date date = df.parse(dateString);
//将日期转换为时间戳
Calendar cal=Calendar.getInstance();
cal.setTime(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
return sdf.format(new Date((cal.getTimeInMillis())));

} catch (Exception ex) {

}
return dateString;
}

简易方法
public String getCreateTime()
{
try
{
SimpleDateFormat sourceFormat = new SimpleDateFormat("yyyyMMddHHmmss");

SimpleDateFormat destFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

return destFormat.format(sourceFormat.parse(createTime));

} catch (Exception e)
{
LOGGER.error("createTime format is error.", e);
}

return createTime;
}

相关文章

    暂无相关文章
相关栏目:

用户点评