举个例子
把String 型 20060823143437  如何转换成日期的这样格式:2006-08-23 14:34:37谢谢

解决方案 »

  1.   

    用SimpleDateFormat转两次但得到的仍是字符串
      

  2.   

    给你个例子:
    String myString = "2005/12/20";SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
    Date d = sdf.parse(myString);
      

  3.   

    你的string可以按如下格式转成时间
    String myString = "20060823143437";SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
    Date d = sdf.parse(myString);转成时间后,再用SimpleDateFormat转成2006-08-23 14:34:37
    匹配的时间格式:yyyy-MM-dd HH:mm:ss
      

  4.   

    public static void main(String[] args) throws Exception {
    String s = "20060823143437";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    Date d = sdf.parse(s);
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String ss = sdf2.format(d);
    System.out.println(ss);
    }
      

  5.   

    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    Date date = formatter.parse("20060823143437");
    SimpleDateFormat formatternext = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    formatternext.parse(formatter.format(date));
      

  6.   

    public static void main(String[] args) throws Exception {
    String s = "20060823143437";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    Date d = sdf.parse(s);
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String ss = sdf2.format(d);
    System.out.println(ss);
    }
    //
    这个ss最后转换不还是 String 型吗?
    我要的是(date)日期型的这样格式:2006-08-23 14:34:37能否在具体点!谢谢!
    小弟刚学习,请见亮!
      

  7.   

    日期型的数据只保存日期的信息,格式信息不保存
    因此,如果你只需要日期型数据,则第一次转换结束的时候已经可以了String myString = "20060823143437";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
    Date d = sdf.parse(myString);等需要输出的时候,再用后面的代码将其输出
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String ss = sdf2.format(d);
    System.out.println(ss);
      

  8.   

    如果不行可以这样试试,就是有点麻烦
    public Date getDate(String time) {
        Calender c = Calender.getInstence();
        c.set(Integer.parseInt(time.substring(0,3)), Integer.parseInt(time.substring(4,5)),Integer.parseInt(time.substring(6,7)), Integer.parseInt(time.substring(8,9)),Integer.parseInt( time.substring(10,11)), Integer.parseInt(time.substring(12,13))) ;
        return c.getTime();
    }
      

  9.   

    哦,我刚才试了一下,我往数据库插的时候 他要求必须用java.sql.Date中的类型,而我们上面
    转化来的是java.util.Date中的类型,那我怎么把他从java.util.Date转化到java.sql.Date的类型呀!最好详细点的例子!
    谢谢
      

  10.   

    String myString = "20060823143437";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss", Locale.CHINA);
    Date d = sdf.parse(myString);java.sql.Date sd = new java.sql.Date(d.getTime());