date日期类型与String类型的相互转化

Code:
  1. import java.sql.Timestamp;  
  2. import java.text.DateFormat;  
  3. import java.text.ParseException;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Date;  
  6.   
  7. import org.apache.commons.logging.Log;  
  8. import org.apache.commons.logging.LogFactory;  
  9.   
  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. }  
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值