在系统中,我需要写一个字符串转化为 sql.Date类的方法。
由于目前没有确定字符串的具体格式,因此,我想把简单的几种格式的字符串都可以处理。YYYYMMDD,YYYY-MM-DD, YYYY/MM/DD.
方法的代码如下:
private Date str2Date(String strDate){
Date ret = null;

if (strDate == null){
return null;
}

if (strDate.length() == 8){
strDate = strDate.substring(0,4) + "-" + strDate.substring(4,6) + "-" + strDate.substring(6,8);
ret = Date.valueOf(strDate);
}else if (strDate.length() == 10){
System.out.println("Befe Trans StrDate is :" + strDate);
strDate.replace('/','-');
System.out.println("Translated StrDate is :" + strDate);
ret = Date.valueOf(strDate); 
}
return ret;
}我觉得按照以上的代码应该对三种格式的都可以处理。但是,结果是只可以处理前两种格式,在处理最后一种格式的strDate.replace('/','-'); 语句并没有起作用。 不知道什么原因,如何改正!因为我所在项目的JDK是1.3,所以,1.4支持的一些方法在这里不能使用。

解决方案 »

  1.   

    strDate.replace('/','-')对YYYY/MM/DD.类型的字符串才有作用,对其他格式的都不起作用的
      

  2.   


    System.out.println("Translated StrDate is :" + strDate.replace('/','-'));
    =======
    strDate.replace的返回值是你要的东西
      

  3.   

    to jlusdy(LOST)
    抱歉,受苦了。哈哈。  
    没有注意到漏写了代码。给分,给分!to OnlyFor_love(『不给我分 就剪掉楼主小鸡几』) :
    没有读懂代码, 不给分!