常常在開發的過程當中遇到這樣的問題,從數據庫中讀出來的數據須要轉換爲對像或者java bean,此時常用到setDate(Date date);這樣的方法。感受這是個很簡單而又難受的事情,在這裏浪費時間真的是不該該。我這裏就記錄下我使用過的,方便之後查閱使用。java
例以下面的setter方法:sql
private String userId; private boolean isLogin; private Date loginTime; public void setLoginTime(Date loginTime) { Date oldLoginTime = this.loginTime; this.loginTime = loginTime; } public void setIsLogin(boolean isLogin) { boolean oldIsLogin = this.isLogin; this.isLogin = isLogin; } public void setUserId(String userId) { String oldUserId = this.userId; this.userId = userId; }
使用到的String轉換爲date方法數據庫
import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class DateUtil { /** * set方法中是timestamp類型 * new Timestamp(s) * @return s */ public Timestamp newStampTime(){ long s=System.currentTimeMillis(); return new Timestamp(s); } /** * timeStamp轉換爲String * @return */ public String timeStampToString(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定義格式,不顯示毫秒 Timestamp now = new Timestamp(System.currentTimeMillis());//獲取系統當前時間 String str = df.format(now); return str; } /** * String 轉換爲timestamp * @return */ public Timestamp StringTotimeStamp(){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = df.format(new Date()); Timestamp ts = Timestamp.valueOf(time); return ts; } public Date myStringTotimeStamp(String dateString){ int length=dateString.length(); if(length>10){ DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date today=new Date(); try { today = df.parse("2009-11-11"); //String ts=df.format(today); } catch (ParseException e) { e.printStackTrace(); } return today; }else { DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Date today=new Date(); try { today = df.parse("2009-11-11"); //String ts=df.format(today); } catch (ParseException e) { e.printStackTrace(); } return today; } } /** * String 轉換爲timestamp * String "2016-5-25" 轉換爲 Timestamp * @return */ public Timestamp StringTotimeStamp(String dateString){ SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); StringBuffer ds=new StringBuffer(dateString); if(dateString.length()>10){ Timestamp ts = Timestamp.valueOf(dateString); return ts; }else{ ds.append(" 00:00:00"); Timestamp ts = Timestamp.valueOf(ds.toString()); return ts; } } /** * 獲取當前時間字符串 * eg:2016-10-11 16:57:52 **/ public String formateDateString(){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd H:m:s"); String dateString=format.format(new Date()); return dateString; } /** * 指定時間和當前時間比較大小 */ public boolean compareTime(String t1){ if(t1.length()<=10){ t1=t1+" 23:59:59"; } Date currentTime = new Date();// 當前時間 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowtime = formatter.format(currentTime); Calendar now = Calendar.getInstance(); Calendar c1 = Calendar.getInstance(); //String t1 = "2016-02-29 00:30:00"; try { now.setTime(formatter.parse(nowtime)); c1.setTime(formatter.parse(t1)); } catch (ParseException e) { e.printStackTrace(); } int result1 = now.compareTo(c1);// 比開始時間小,未開始 System.out.println("結果:"+result1); if(result1>0){ return false; }else{ return true; } } }