日期:2014-05-20 浏览次数:20822 次
//格式固定的话不需要想太复杂吧 String[] strArr = "2011_09_14_15_58_28".split("_"); System.out.println(new StringBuilder(strArr[0]).append("-").append( strArr[1]).append("-").append(strArr[2]).append(" ").append( strArr[3]).append(":").append(strArr[4]).append(":").append( strArr[5]).toString());
------解决方案--------------------
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf2.format(sdf.parse("2011_09_14_15_58_28"));
------解决方案--------------------
import java.text.*; import java.util.*; public class Test { public static void main(String[] args) throws Exception { String s = "2011_09_14_15_58_28"; SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); Date date = format.parse(s); format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(format.format(date)); } }
------解决方案--------------------
for example
String s = "2011_09_14_15_58_28"; String[] sa = s.split("_"); s = String.format("%s-%s-%s %s:%s:%s", sa[0], sa[1], sa[2], sa[3], sa[4], sa[5]); or StringBuilder buf = new StringBuilder(); for (int i=0; i<sa.length; i++) { if (i < 2) { buf.append(sa[i]).append("-"); } else if (i == 2) { buf.append(s[i]).append(" "); } else if (i < 5) { buf.append(s[i]).append(":"); } else { buf.append(sa[i]); } } s = buf.toString();
------解决方案--------------------
个人觉得2楼好,不用很多对象,很多对象,简单实际.
------解决方案--------------------
String time = new String("2011_09_14_15_58_28"); SimpleDateFormat a = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); try { Date date = a.parse(time); System.out.println("将字符串转化为时间是" + date); } catch (ParseException e) { throw new RuntimeException(""); }
------解决方案--------------------
public static void main(String[] args) { String a = "2011_09_14_15_58_28"; String b = a.substring(0, 10).replace('_', '-') + " " + a.substring(11).replace('_', ':'); System.out.println(b); }