java中setDate(Date date)方法和String與Date之間的轉換

來源:互聯網
上載者:User

標籤:系統   dateutil   string轉換   date   .text   轉換   之間   code   方法   

經常在開發的過程中遇到這樣的問題,從資料庫中讀出來的資料需要轉換為對像或者java bean,此時經常使用到setDate(Date date);這樣的方法。感覺這是個很簡單而又難受的事情,在這裡浪費時間真的是不應該。我這裡就記錄下我使用過的,方便以後查閱使用。

例如下面的setter方法:

    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;                }               }          }

 

java中setDate(Date date)方法和String與Date之間的轉換

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.