Java TIMESTAMP 有尾随零,如何删除?
我正在使用 Java 和 hibernate,并且正处于学习一切如何工作的早期阶段。我进展缓慢,但在 Java 中的日期方面遇到了一些麻烦。
我当前的问题是,当我从 MySQL 数据库获取时间戳时,它的末尾似乎有一些额外的零。当我将其传递给 PHP(是的,它是我们正在编写的 Java/PHP 应用程序)时,格式化日期会以错误的结果结束。
这是我看到的:
2000-08-10 14:09:21 // Time in database
965912961000 // Timestamp from Java of this time
965912961 // The PHP equivalent of the database timestamp - using strtotime()
在 Java 模型中,我有这样的变量设置:
@Temporal (TemporalType.TIMESTAMP)
private Date logTime
所以我的问题是如何摆脱三个尾随零?显然我可以用 PHP 删除它们,但如果可能的话我宁愿它来自后端 Java。
I'm using Java with hibernate and am in the early stages of learning how everything works. I'm getting there slowly but I have had some trouble with dates in Java.
My current problem is that when I get a timestamp from a MySQL database it seems to have a load of extra zeros on the end. When I pass this through to PHP (yes its a Java/PHP application we're writing) then formatting the date ends with an incorrect result.
Here's what I see:
2000-08-10 14:09:21 // Time in database
965912961000 // Timestamp from Java of this time
965912961 // The PHP equivalent of the database timestamp - using strtotime()
In the Java model I have the variable setup like this:
@Temporal (TemporalType.TIMESTAMP)
private Date logTime
So my question is how do I get rid of the three trailing zeros? Obviously I could remove these with PHP but I'd rather it came from the back-end Java if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前尚不清楚为什么要将
Date
格式化为整数,但这是一个简单的事实,JavaDate
以毫秒为单位 - 而 PHP 可能是以秒为单位(自 Unix 纪元以来)。不过,这是一种内部表示,大多数时候您不应该关心它。如果您要传输该整数,请随意除以千,将其从基于毫秒的表示转换为基于秒的表示。
如果您能更清楚地了解如何看待整数,我们也许可以提供更多帮助。
It's not clear why you're formatting the
Date
into an integer, but it's a simple fact of life that a JavaDate
is measured in milliseconds - whereas presumably a PHP one is measured in seconds (since the Unix epoch).That's an internal representation though, which most of the time you shouldn't care about. If you're transmitting that integer, feel free to just divide by a thousand to convert it from a milliseconds-based representation to a seconds-based representation.
If you can be clearer about how you're seeing the integer to start with, we may be able to help more.