date日期类型与String类型的相互转化(转自http://blog.csdn.net/shy_snow/article/details/6501043)

01.import java.sql.Timestamp;  
02.import java.text.DateFormat;  
03.import java.text.ParseException;  
04.import java.text.SimpleDateFormat;  
05.import java.util.Date;  
06.  
07.import org.apache.commons.logging.Log;  
08.import org.apache.commons.logging.LogFactory;  
09.  
10./** 
11. * @author zxw 
12. * 
13. */  
14.public class DateUtil {  
15.    private static final Log log = LogFactory.getLog(DateUtil.class);  
16.  
17.    /** 
18.     * 将时间字符串转换为Date类型 
19.     * @param dateStr 
20.     * @return Date 
21.     */  
22.    public static Date toDate(String dateStr) {  
23.        Date date = null;  
24.        SimpleDateFormat formater = new SimpleDateFormat();  
25.        formater.applyPattern("yyyy-MM-dd");  
26.        try {  
27.            date = formater.parse(dateStr);  
28.        } catch (ParseException e) {  
29.            e.printStackTrace();  
30.        }  
31.        return date;  
32.    }  
33.  
34.    /** 
35.     * 按照提供的格式将字符串转换成Date类型 
36.     * @param dateStr 
37.     * @param formaterString 
38.     * @return 
39.     */  
40.    public static Date toDate(String dateStr, String formaterString) {  
41.        Date date = null;  
42.        SimpleDateFormat formater = new SimpleDateFormat();  
43.        formater.applyPattern(formaterString);  
44.        try {  
45.            date = formater.parse(dateStr);  
46.        } catch (ParseException e) {  
47.            e.printStackTrace();  
48.        }  
49.        return date;  
50.    }  
51.  
52.    /** 
53.     * 将Date类型时间转换为字符串 
54.     * @param date 
55.     * @return 
56.     */  
57.    public static String toString(Date date) {  
58.  
59.        String time;  
60.        SimpleDateFormat formater = new SimpleDateFormat();  
61.        formater.applyPattern("yyyy-MM-dd");  
62.        time = formater.format(date);  
63.        return time;  
64.    }  
65.  
66.    /** 
67.     * 按照参数提供的格式将Date类型时间转换为字符串 
68.     * @param date 
69.     * @param formaterString 
70.     * @return 
71.     */  
72.    public static String toString(Date date, String formaterString) {  
73.        String time;  
74.        SimpleDateFormat formater = new SimpleDateFormat();  
75.        formater.applyPattern(formaterString);  
76.        time = formater.format(date);  
77.        return time;  
78.    }  
79.  
80.    /** 
81.     * method 将字符串类型的日期转换为一个timestamp(时间戳记java.sql.Timestamp) 
82.     * @param dateString 
83.     *            需要转换为timestamp的字符串 
84.     * @return dataTime timestamp 
85.     */  
86.    public final static java.sql.Timestamp string2Time(String dateString)  
87.            throws java.text.ParseException {  
88.        DateFormat dateFormat;  
89.//      dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS",  
90.//              Locale.ENGLISH);// 设定格式  
91.         dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm");  
92.        dateFormat.setLenient(false);  
93.        java.util.Date timeDate = dateFormat.parse(dateString);// util类型  
94.        java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型  
95.        return dateTime;  
96.    }  
97.  
98.    /** 
99.     * method 将字符串类型的日期按照转换为一个timestamp(时间戳记java.sql.Timestamp) 
100.     *  
101.     * @param dateString 需要转换为timestamp的字符串 
102.     * @param formaterString dateString字符串的解析格式 
103.     * @return 
104.     * @throws java.text.ParseException 
105.     */  
106.    public final static java.sql.Timestamp string2Time(String dateString,  
107.            String formaterString) throws java.text.ParseException {  
108.        DateFormat dateFormat;  
109.        dateFormat = new SimpleDateFormat(formaterString);// 设定格式  
110.        // dateFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");  
111.        dateFormat.setLenient(false);  
112.        java.util.Date timeDate = dateFormat.parse(dateString);// util类型  
113.        java.sql.Timestamp dateTime = new java.sql.Timestamp(timeDate.getTime());// Timestamp类型,timeDate.getTime()返回一个long型  
114.        return dateTime;  
115.    }  
116.  
117.    public static void main(String[] args) throws ParseException {  
118.        String t = DateUtil.toString(new Date());  
119.        System.out.println(t);  
120.        Date date = DateUtil.toDate("2010-06-17");  
121.        System.out.println(date);  
122.  
123.//      String sToTimestamp = "2005-8-18 14:21:12.123";// 用于转换成java.sql.Timestamp的字符串  
124.        String sToTimestamp = "2005-8-18 14:21";// 用于转换成java.sql.Timestamp的字符串  
125.        Timestamp Timestamp = string2Time(sToTimestamp);  
126.        System.out.println(Timestamp);  
127.  
128.    }  
129.  
130.}  

 

posted @ 2013-05-03 09:54  觉信  阅读(487)  评论(0编辑  收藏  举报