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

Code:
  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4.   
  5. import org.apache.log4j.Logger;  
  6.   
  7. import com.fourstar.log4j.HelloLog4j;  
  8.   
  9. public class DateUtil {  
  10.     private static final Logger log = Logger.getLogger(HelloLog4j.class);//获得当前类      
  11.     public static Date toDate(String dateStr) {  
  12.         Date date = null;  
  13.         SimpleDateFormat formater = new SimpleDateFormat();  
  14.         formater.applyPattern("yyyy-MM-dd hh:m:ss");  
  15.         try {  
  16.             date = formater.parse(dateStr);  
  17.         } catch (ParseException e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.         return date;  
  21.   
  22.     }  
  23.   
  24.     public static Date toDate(String dateStr, String f) {  
  25.         Date date = null;  
  26.         SimpleDateFormat formater = new SimpleDateFormat();  
  27.         formater.applyPattern(f);  
  28.         try {  
  29.             date = formater.parse(dateStr);  
  30.         } catch (ParseException e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.         return date;  
  34.   
  35.     }  
  36.   
  37.     public static String toString(Date date) {  
  38.   
  39.         String time;  
  40.         SimpleDateFormat formater = new SimpleDateFormat();  
  41.   
  42.         formater.applyPattern("yyyy-MM-dd hh:m:ss");  
  43.         time = formater.format(date);  
  44.   
  45.         return time;  
  46.     }  
  47.   
  48.     public static String toString(Date date, String f) {  
  49.         String time;  
  50.         SimpleDateFormat formater = new SimpleDateFormat();  
  51.         formater.applyPattern(f);  
  52.         time = formater.format(date);  
  53.         return time;  
  54.     }  
  55.   
  56.     public static void main(String[] args) {  
  57.         Date todayDate = new Date();//date类型  
  58.         System.out.println(todayDate);  
  59.         String t = DateUtil.toString(todayDate,"yyyy-MM-dd hh:m:ss");//string类型  
  60.         System.out.println(t);  
  61.         todayDate = DateUtil.toDate(t, "yyyy-MM-dd hh:m:ss");//date类型  
  62.         System.out.println(todayDate);  
  63.     }  
  64.   
  65. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值