導航:首頁 > 編程語言 > java插入timestamp

java插入timestamp

發布時間:2023-06-19 07:24:38

⑴ 怎樣在java中將日期轉化插入到資料庫

Java中將日期轉化插入到資料庫:

(Stringdate_str){
try{
Calendarcal=Calendar.getInstance();
java.sql.Timestamptimestampnow=newjava.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
ParsePositionpos=newParsePosition(0);
java.util.Datecurrent=formatter.parse(date_str,pos);
timestampnow=newjava.sql.Timestamp(current.getTime());
returntimestampnow;
}
catch(NullPointerExceptione){
returnnull;
}
}
//格式以上面不一樣yyyy-MM-dd
(Stringdate_str){
try{
Calendarcal=Calendar.getInstance();
java.sql.Timestamptimestampnow=newjava.sql.Timestamp(cal.getTimeInMillis());
SimpleDateFormatformatter=newSimpleDateFormat("yyyy-MM-dd");
ParsePositionpos=newParsePosition(0);
java.util.Datecurrent=formatter.parse(date_str,pos);
returncurrent;
}
catch(NullPointerExceptione){
returnnull;
}
}
插入資料庫,用java.sql.PreparedStatement即可。

⑵ mysql中timestamp類型 我設置了默認值 為什麼java中添加時間類型還是null 在資料庫中可以

你用hibernate的話,除非你在保存的時候用java指定他的值,否則本身還是NULL , 你要麼就將這個列刪除,由資料庫自己去管理就可以了!

⑶ timestamp mysql java 日期插入不了

.0表示毫秒在0-999之間取值,可以用格式化方式來解決:
import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

formatStr =yyyy-MM-dd HH:mm:ss";//輸出格式
SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
try {
Date d = formatter.parse(「2008-04-26 13:33:26.0」);
} catch (ParseException e) {
logger.error("format Date error:" + dt + ".error msg:"
+ e.getMessage());
e.printStackTrace();
}
}
return d;

閱讀全文

與java插入timestamp相關的資料

熱點內容
安卓系統藍牙耳機如何用 瀏覽:719
為什麼微信不能給appstore充值 瀏覽:493
程序員的保護動物 瀏覽:272
程序員遇到問題去哪個網站 瀏覽:531
安卓手機空格鍵連續輸入怎麼取消 瀏覽:520
壓縮空氣管道流量計 瀏覽:564
ug編程高級教程 瀏覽:177
什麼叫做伺服器已滿 瀏覽:37
暑假哪有教演算法的 瀏覽:136
密碼學的根基是加密 瀏覽:662
stata方差檢驗命令 瀏覽:337
解壓後文件夾里的內容丟失 瀏覽:715
解壓無敵視頻 瀏覽:690
什麼是伺服器辨認不了 瀏覽:129
java如何調用類方法 瀏覽:483
管理孩子的app叫什麼 瀏覽:546
壓縮活動軌跡 瀏覽:674
6米梁加密筋 瀏覽:79
怎麼學好ps如何學好編程 瀏覽:301
c編譯器廠商 瀏覽:114