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

熱點內容
伺服器2016自動備份怎麼取消 瀏覽:838
dos攻擊命令大全 瀏覽:840
夢世界伺服器怎麼擴大領地 瀏覽:966
解壓breathe 瀏覽:431
webview調用java 瀏覽:346
android待機流程 瀏覽:854
python直接退出程序 瀏覽:845
百戰程序員收費標准 瀏覽:776
時鍾置換演算法指針變化規則 瀏覽:248
微信加密能否改密碼 瀏覽:107
android許可權組 瀏覽:175
2017單片機 瀏覽:481
讓孩子感興趣編程的電影 瀏覽:264
用顏料製作脆皮解壓球 瀏覽:938
火箭解壓器 瀏覽:78
cnet中級程序員面試題 瀏覽:192
單片機怎麼做人臉識別 瀏覽:157
監獄辦理工商銀行app怎麼辦呢 瀏覽:822
c語言寫編程時需要用什麼輸入法 瀏覽:593
生發程序員 瀏覽:167