怎么样才能将字符串Tue May 23 00:00:00 CST 2006转换成一般的Date,或者转换成2006-05-23这样的字符串?

解决方案 »

  1.   

    Date now = new Date();
    SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    String NowStr = dataFormat.format(now);
      

  2.   

    我现在就是一个字符串:Tue May 23 00:00:00 CST 2006,不是一个Date对象,有没有API可以直接分析这个字符串,来得到一个Date对象?
      

  3.   

    如果拿到手的只有字符串的话,用substring提取吧,也不是很麻烦
    否则就采用SimpleDateFormat,用起来很爽哦
      

  4.   

    SimpleDateFormat我用过的,转换不成功,看来只有自己写程序分析实现功能了。
    不明白sun为什么不也加个转换这样个是的API呢!
      

  5.   

    Date d = new Date(Date.parse("Tue May 23 00:00:00 CST 2006"));
            
    System.out.println(d);结果不对,应该是时区的关系。自己再查资料吧
      

  6.   

    谢谢各位,我用了个蠢办法解决问题了:
    if(data != null && data.length() == 28 && data.indexOf("CST") == 20){
    String year = data.substring(24, 28);
    String date = data.substring(8, 10);
    String temp = data.substring(4,7);
    if(temp.equals("Jan")){
    data = year + "-" + "01" + "-" + date;
    }
    if(temp.equals("Feb")){
    data = year + "-" + "02" + "-" + date;
    }
    if(temp.equals("Mar")){
    data = year + "-" + "03" + "-" + date;
    }
    if(temp.equals("Apr")){
    data = year + "-" + "04" + "-" + date;
    }
    if(temp.equals("May")){
    data = year + "-" + "05" + "-" + date;
    }
    if(temp.equals("Jun")){
    data = year + "-" + "06" + "-" + date;
    }
    if(temp.equals("Jul")){
    data = year + "-" + "07" + "-" + date;
    }
    if(temp.equals("Aug")){
    data = year + "-" + "08" + "-" + date;
    }
    if(temp.equals("Sep")){
    data = year + "-" + "09" + "-" + date;
    }
    if(temp.equals("Oct")){
    data = year + "-" + "10" + "-" + date;
    }
    if(temp.equals("Nov")){
    data = year + "-" + "11" + "-" + date;
    }
    if(temp.equals("Dec")){
    data = year + "-" + "12" + "-" + date;
    }
    }