java.util.Date和java.sql.Date的转换   Leave a comment

  1.  法一:  
  1. java.text.SimpleDateFormat bartDateFormat =  
  2.                                                 new SimpleDateFormat("yyyy-MM-dd");  
  3.         String dateStringToParse = "2007-7-12";  
  4.         try {  
  5.               
  6.             java.util.Date date = bartDateFormat.parse(dateStringToParse);  
  7.             java.sql.Date sqlDate = new java.sql.Date(date.getTime());  
  8.             System.out.println("此时sqlDate的类型:" + sqlDate.getClass().toString());  
  9.               
  10.         } catch (Exception ex) {  
  11.             System.out.println(ex.getMessage());  
  12.         }  

 

法二:

  1. String strDate = "2009-06-09";  
  2.         java.util.StringTokenizer st = new StringTokenizer(strDate, "-");  
  3.           
  4.         java.sql.Date date = new java.sql.Date(  
  5.                                         Integer.parseInt(st.nextToken()),  
  6.                                         Integer.parseInt(st.nextToken()),   
  7.                                         Integer.parseInt(st.nextToken()));  
  8.         System.out.println("此时date的类型:"+date.getClass());  

 

结果:

class java.sql.Date

说明:ok

 

扩展:查看java.sql.Dated的源码:

 

  1. @Deprecated  
  2.     public Date(int year, int month, int day) {  
  3.     super(year, month, day);  
  4.     } 


Posted 2009年11月9日 by gw8310 in Computers and Internet

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: