从毫秒创建 GregorianCalendar 实例
我有一个以毫秒为单位的特定时间(在 Timestamp
对象中),我想用它来创建 GregorianCalendar
对象。我怎样才能做到这一点?
编辑:我该如何做相反的事情?
I have a certain time in milliseconds (in a Timestamp
object) and I want to use it to create a GregorianCalendar
object. How can I do that?
EDIT: How do I do the reverse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信这是可行的,尽管它可能不是最好的方法:
I believe this works, although it may not be the best approach:
获取 GregorianCalendar 对象而不是 Calendar 对象。就像 Michael 的回答所提供的那样,您还可以执行以下操作:
这假定 UTC 纪元时间戳。
To get a GregorianCalendar object and not a Calendar object. Like Michael's answer provides, you can also do the following:
This assumes a UTC epoch timestamp.
只需使用 java.sql.Timestamp
timestamp
获取 GregorianCalendar 和 setTime 的实例:编辑:
正如 peterh 指出的,
GregorianCalendar.getInstance()
不会提供GregorianCalendar< /code> 默认情况下,因为它继承自
Calendar.getInstance()
,它可以在某些安装上提供BuddhistCalendar
等。要确保使用GregorianCalender
,请改用new GregorianCalendar()
。Just get an instance of GregorianCalendar and setTime with your java.sql.Timestamp
timestamp
:Edit:
As peterh pointed out,
GregorianCalendar.getInstance()
will not provide aGregorianCalendar
by default, because it is inherited fromCalendar.getInstance()
, which can provide for example aBuddhistCalendar
on some installations. To be sure to use aGregorianCalender
usenew GregorianCalendar()
instead.