我在将 sql 时间戳转换为字符串时遇到了一些麻烦,它们的输出不同。
@Temporal(javax.persistence.TemporalType.TIMESTAMP)
private Date auctionStart;
public void setTime(){
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String aucStartString = f.format(auctionStart);
}
我的拍卖开始日期打印:2017 年 4 月 4 日 17:14:10 当我将其转换为字符串时,它会打印:2017-04-04 05:14:10
看起来小时数正在转换为 12 小时格式,有什么想法如何纠正它吗?
最佳答案
尝试使用 H
而不是 h
来表示从 Class SimpleDateFormat
开始的小时数。 javadoc。
- H 一天中的小时 (0-23) 数字 0
- 一天中的 k 小时(1-24)数字 24
- 上午/下午 K 小时(0-11)数字 0
h 上午/下午 (1-12) 数字 12
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
更新
Date auctionStart = new Date(2017-1900,4,4,17,14,10);
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String aucStartString = f.format(auctionStart);
System.out.println(aucStartString);
打印
2017-05-04 17:14:10
关于java - 将java sql时间戳转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43219680/