如果一个日期字符串"2005-06-09"如何传化成Date类型

 所属目录:Java   |   类型:技术问答   |   时间:2007-05-21
 问题:

注:1.4版不能直接用字符串建  
  我自己想出了一个方法 但感觉太麻烦了   
  大家帮我想想别的啊

· 网友精彩回答:

发表者:free_card

string   date="2005-05-20   11:20:00";  
  simpledateformat   myfmt=new   simpledateformat("yyyy-mm-dd   hh:mm:ss");  
  try{  
          date   newdate=myfmt.parse(date);  
          return   newdate;  
      }catch(exception   e){  
          e.printstacktrace();  
          return   null;  
      }  
 

发表者:roland001981

import   java.text.*;  
  import   java.util.*;  
   
  public   class   date  
  {  
   
  public   static   void   main(string   args[])   throws   parseexception  
  {  
  calendar   cal   =   calendar.getinstance();    
  string   str   =   "2005-06-09";  
  //dateformat   df=new   simpledateformat("yy-mm-dd   hh:mm:ss");  
  dateformat   df=new   simpledateformat("yy-mm-dd");  
  date   d   =   df.parse(str);  
  //system.out.println(d);  
  }  
  }

发表者:roland001981

补充一点  
  import   java.text.*;  
  import   java.util.*;  
   
  public   class   date  
  {  
   
  public   static   void   main(string   args[])   throws   parseexception  
  {  
  calendar   cal   =   calendar.getinstance();    
  string   str   =   "2005-06-09";  
  //dateformat   df=new   simpledateformat("yy-mm-dd   hh:mm:ss");  
  dateformat   df=new   simpledateformat("yyyy-mm-dd");  
  date   d   =   df.parse(str); //转为date  
  string   value   =   df.format(d); //转为string  
  system.out.println(a);  
  }  
  }  
 

发表者:power1128

如果你的日期类型要写入sql的话,一般用这种  
  java.sql.date   temp_date=java.sql.date.valueof(datestring);

.
处理 SSI 文件时出错
© 2006-2008 All Rights Reserved