String轉Longjava
Long long=new SimpleDateFormat("yyyyMMddHHmmss").parse(String).getTime();
Long轉String sql
String string = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(Long));
String轉Timestamp數據庫
Timestamp.valueOf(String)
Timestamp轉Stringorm
Timestamp ts = new Timestamp(System.currentTimeMillis()); String string=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(ts);
String轉Dateblog
Date date=new SimpleDateFormat("yyyyMMddHHmmss").parse(String)
Date轉Stringget
String string=new SimpleDateFormat("yyyyMMddHHmmss").format(date)
Date轉Longstring
Long long=date.getTime();
Long轉Dateform
String string=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(Long)); Date date=new SimpleDateFormat("yyyyMMddHHmmss").parse(string);
Date轉Timestampclass
Timestamp ts = new Timestamp(date.getTime());
Timestamp轉Datedate
Timestamp ts = new Timestamp(System.currentTimeMillis()); Date date=new Date(); date=ts;
Long轉Timestamp
Long time1=System.currentTimeMillis(); Timestamp time=new Timestamp(time1);
Timestamp轉Long
long now=t.getDateTime();
注意:若是數據庫是dateTime類型的,即便java代碼中聲明的是Timestamp,但仍是用.getTime()方法來獲取這個Long類型的時間。
MySql的時間類型有 Java中與之對應的時間類型
date java.sql.Date
Datetime java.sql.Timestamp
Timestamp java.sql.Timestamp
Time java.sql.Time
Year java.sql.Date
因此,若是數據庫是dateTime,則先用java轉換成timestamp類型
Date date = new Date();//得到系統時間. String nowTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);//將時間格式轉換成符合Timestamp要求的格式. Timestamp xx = Timestamp.valueOf(nowTime);
而後存入數據庫