从毫秒创建 GregorianCalendar 实例

发布于 2024-10-07 09:50:38 字数 147 浏览 3 评论 0原文

我有一个以毫秒为单位的特定时间(在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-10-14 09:50:39
Timestamp timestamp = new Timestamp(23423434);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());
Timestamp timestamp = new Timestamp(23423434);
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTimeInMillis(timestamp.getTime());
呆萌少年 2024-10-14 09:50:39

我相信这是可行的,尽管它可能不是最好的方法:

import java.sql.Date;
import java.sql.Timestamp;
import java.util.GregorianCalendar;

public class TimestampToGregorianCalendar {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Timestamp t = new Timestamp(12356342); // replace with existing timestamp
        Date d = new Date(t.getTime());
        Calendar gregorianCalendar = GregorianCalendar.getInstance();
        gregorianCalendar.setTime(d);
    }

}

I believe this works, although it may not be the best approach:

import java.sql.Date;
import java.sql.Timestamp;
import java.util.GregorianCalendar;

public class TimestampToGregorianCalendar {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Timestamp t = new Timestamp(12356342); // replace with existing timestamp
        Date d = new Date(t.getTime());
        Calendar gregorianCalendar = GregorianCalendar.getInstance();
        gregorianCalendar.setTime(d);
    }

}
飘过的浮云 2024-10-14 09:50:38

获取 GregorianCalendar 对象而不是 Calendar 对象。就像 Michael 的回答所提供的那样,您还可以执行以下操作:

long timestamp = 1234567890;
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(timestamp);

这假定 UTC 纪元时间戳。

To get a GregorianCalendar object and not a Calendar object. Like Michael's answer provides, you can also do the following:

long timestamp = 1234567890;
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(timestamp);

This assumes a UTC epoch timestamp.

忘羡 2024-10-14 09:50:38

只需使用 java.sql.Timestamp timestamp 获取 GregorianCalendar 和 setTime 的实例:

Calendar cal=GregorianCalendar.getInstance();
cal.setTime(timestamp);

编辑:
正如 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:

Calendar cal=GregorianCalendar.getInstance();
cal.setTime(timestamp);

Edit:
As peterh pointed out, GregorianCalendar.getInstance() will not provide a GregorianCalendar by default, because it is inherited fromCalendar.getInstance(), which can provide for example a BuddhistCalendar on some installations. To be sure to use a GregorianCalender use new GregorianCalendar() instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文