日期:2014-05-20  浏览次数:20632 次

字符串转日期格式
20110129 这个是我从数据库里面取到的字符串怎么把它转成2011-09-12这种日期格式???

------解决方案--------------------
Java code

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date date = sdf.parse("20110129");
        sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(date));

------解决方案--------------------
oracle
SQL code

select to_char(to_date('20110129','yyyyMMdd'),'yyyy-MM-dd') from dual

------解决方案--------------------
字符串格式是什么?

如果就是“2011-09-12”,直接DateTimeFormatter.getDateInstance(DateFormat.SHORT).parse()
或者 new SimpleDateFormat("yyyy-MM-dd").parse();

如果是其他的日期格式,换个"yyyy-MM-dd"格式 parse

格式太复杂,就先自己subString,再转
------解决方案--------------------
探讨

Java code

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse("20110129");
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.format……

------解决方案--------------------
楼主可以结贴了吧 ,祝楼主工作顺心
------解决方案--------------------
探讨
Java code


SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = sdf.parse("20110129");
sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(sdf.f……