導航:首頁 > 編程語言 > 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相關的資料

熱點內容
滴滴號碼加密怎麼解除 瀏覽:844
模具編程的職責 瀏覽:941
華為ssh改加密演算法 瀏覽:147
文件夾空白合同 瀏覽:761
pythonwebpy開發 瀏覽:669
不是c編譯器的有 瀏覽:660
win10壓縮包下載 瀏覽:905
逆戰手機app怎麼樣 瀏覽:946
自嗨自我解壓圖片 瀏覽:395
電子書導入kindle哪個文件夾 瀏覽:418
pythontcpserver性能 瀏覽:544
linux文件夾改名 瀏覽:564
單片機開發板是什麼 瀏覽:851
阿里雲伺服器不能截屏 瀏覽:866
如何自己製作聯想伺服器 瀏覽:843
停車場規劃演算法 瀏覽:923
深蹲PDF 瀏覽:908
數據科學包python 瀏覽:849
程序員學習視頻網站 瀏覽:125
吃雞游戲如何安卓轉蘋果 瀏覽:188