将Apple的Cocoa框架Epoch(reference_date)转换为Java Date
iPhone 客户端将日期作为 Apple 的 Cocoa 框架 Epoch(自 2001 年以来的 ms)发送到 Java 服务器,当您转换为 java 日期(自 1970 年以来)时,它会转换为 2011 年的 1980 年,在 Cocoa 日期上添加 30 年将转换?或者我可以使用任何其他方法进行此转换吗?
an iPhone client send a date to a Java server as Apple's Cocoa framework Epoch (which the ms since 2001) and when you converted to java date (since 1970) it convert as 1980 for the year 2011, is adding 30 year to Cocoa date will converted ? or is there any other methode that i could use for this conversion ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议通过添加适当的秒数来调整 Unix 纪元时间(自 1970 年以来)的秒数,而不是增加年份。我并不乐观,但我的直觉告诉我,在某些情况下,改变年份本身就是错误的(闰年和对日历的小调整等)。
如果您需要将自 2001 年 1 月 1 日以来的秒数相加,您要查找的数字是 +978307200。如果您需要毫秒,只需乘以 1000。
作为示例,我有一些代码需要检测 Apple 与 Unix 纪元日期(自纪元以来的秒数),并将它们转换为构造函数中自 Unix 纪元以来花费的毫秒数的 Java 日期:
I would recommend adjusting the seconds to the Unix epoch time (since 1970) by adding the appropriate number of seconds, instead of bumping up the year. I'm not positive, but my instincts tell me that bumping the year itself will be wrong in some cases (with leap years and small adjustments to the calendar, etc).
If you need to add seconds to seconds since Jan 1 2001, the number you're looking for is +978307200. If you need millis, just multiply by 1000.
As an example, I had some code that needed to detect Apple vs Unix epoch dates in seconds since epoch, and convert them into Java Dates that took milliseconds since Unix epoch in the constructor: