JAVA行军

Good Luck

Date和String的转换

 

 1import java.text.DateFormat; 
 2import java.text.SimpleDateFormat; 
 3import java.text.ParseException; 
 4import java.util.Calendar; 
 5import java.util.Date; 
 6
 7
 8DateFormat format = new SimpleDateFormat("yyyy-MM-dd");         
 9            Date date = null;    
10            String str = null;                  
11                        
12            // String转Date    
13            str = "2007-9-26";          
14            try {    
15                date = format.parse(str);  // Wed sep 26 00:00:00 CST 2007    
16            }
 catch (ParseException e) {    
17                e.printStackTrace();    
18            }
    
19                        
20            date = java.sql.Date.valueOf(str);  // 只保留日期部分,返回的是java.sql.Date  2007-9-26    
21                        
22            // Date转String    
23            date = new Date();   // Wed sep 26 18 17:14:01 CST 2007       
24            str = format.format(date);  // 2007-9-26    
25                        
26            format = DateFormat.getDateInstance(DateFormat.SHORT);    
27            str = format.format(date);  // 07-9-26    
28                        
29            format = DateFormat.getDateInstance(DateFormat.MEDIUM);    
30            str = format.format(date);  // 2007-9-26   
31                        
32            format = DateFormat.getDateInstance(DateFormat.FULL);    
33            str = format.format(date);  // 2007年9月26日 星期三   

posted on 2007-09-26 17:17 ILOVEYOU 阅读(3875) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(1)

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜