简体   繁体   English

如何从数据库中声明一个保存时间戳值的变量?

[英]How to declare a variable which holds Timestamp value from database?

I have a timestamp in my database. 我的数据库中有一个时间戳。 I want to fetch it from database using JAVA and display it to the user. 我想使用JAVA从数据库中获取它并显示给用户。 How do I declare timestamp in JAVA? 如何在JAVA中声明时间戳? I am using Date, but it is storing yyyy-mm-ddd format. 我正在使用日期,但它存储的是yyyy-mm-ddd格式。 I want yyyy-mm-dd hh:mm:ss format. 我想要yyyy-mm-dd hh:mm:ss格式。

    Date date = siteBean.getAlertTime();

I want the date variable to hold yyyy-mm-dd hh:mm:ss How can I do that? 我希望日期变量保存yyyy-mm-dd hh:mm:ss我该怎么做?

You should consider working with Calendar, as Date is deprecated. 由于不建议使用日期 ,因此您应该考虑使用日历。 The JavaDocs explains nicely how to replace your code with Calendar-methods. JavaDocs很好地解释了如何用Calendar方法替换代码。

Consider fetching the timestamp from your ResultSet as a String. 考虑从ResultSet中以字符串形式获取时间戳。 You can manipulate the String within Java with the help of SimpleDateFormat . 您可以借助SimpleDateFormat在Java中操作String。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM