Java日历日期操作问题

发布于 2024-10-04 23:54:01 字数 560 浏览 4 评论 0原文

我尝试以以下格式获取当前日期/时间,

SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"); 
// should display something similar to  2001.07.04 AD at 12:08:56 PDT
Calendar calendar = Calendar.getInstance();
calendar.set(2009, 2, 4, 10, 22, 44);
format.format( calendar.getTime() );
System.out.println(calendar.getTime());

但它显示为 Wed Mar 04 10:22:44 IST 2009。

第二个问题是我想查看设置时间的毫秒值。

System.out.println(calendar.getTimeInMillis());

但它总是返回当前时间毫秒值,而不是我之前设置的时间。

我在这里缺少什么?

I am try to get the current date/time in in following format,

SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z"); 
// should display something similar to  2001.07.04 AD at 12:08:56 PDT
Calendar calendar = Calendar.getInstance();
calendar.set(2009, 2, 4, 10, 22, 44);
format.format( calendar.getTime() );
System.out.println(calendar.getTime());

But it display as Wed Mar 04 10:22:44 IST 2009.

The second question is I want to see the milliseconds value of the set time.

System.out.println(calendar.getTimeInMillis());

But it always return the current times milliseconds value and not the time I set earlier.

What is I am missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

尬尬 2024-10-11 23:54:01

SimpleDateFormat#format(Object) 方法返回格式化的 String,它不会影响 Calendar#getTime() 方法的返回方式。为此,您应该执行以下操作:

System.out.println(format.format(calendar.getTime()));

The SimpleDateFormat#format(Object) method returns a formatted String, it does not affect how the Calendar#getTime() method returns. To that end you should do a:

System.out.println(format.format(calendar.getTime()));
放肆 2024-10-11 23:54:01

我看到的第一个问题是 format.format(calendar.getTime()); 返回一个字符串,实际上并没有更改日历的格式。您需要将最后两行替换为 System.out.println(format.format(calendar.getTime())); 以使用您指定的格式实际打印出日期(假设您的 SimpleDateFormat 是有效的,但乍一看似乎没问题)。

不幸的是,不确定您遇到的第二个问题,但我有一个地方可供您开始。我不喜欢 Javadoc 中关于 getTime()getTimeInMillis() 的措辞。 getTime() 方法返回“表示此 Calendar 时间值的 Date 对象”,而 getTimeInMillis()返回“当前时间,以 UTC 毫秒为单位,距纪元”。对我来说,措辞上的差异意味着 getTimeInMillis() 不使用 Calendar 对象指定的日期,即使它是一个实例方法。尝试使用 System.out.println(calendar.getTime().getTime()); 来调用 Date 上的 getTime() 方法> 日历的表示,该文档返回 1970 年 1 月 1 日到 Date 对象表示的日期之间的时间(以毫秒为单位)。

The first problem that I see is that format.format(calendar.getTime()); returns a String and doesn't actually change the formatting of the Calendar. You are going to want to replace the last two lines with System.out.println(format.format(calendar.getTime())); to actually print out the date using the format you specify (assuming your SimpleDateFormat is valid, but it looks OK at a quick glance).

Not sure about the second problem you are having, unfortunately, but I have a place for you to start. I'm not a fan of the wording of the Javadocs for getTime() and getTimeInMillis(). The getTime() method returns "a Date object representing this Calendar's time value" while getTimeInMillis() returns "the current time as UTC milliseconds from the epoch". To me, the difference in wording implies that getTimeInMillis() doesn't use the date specified by the Calendar object, even though it is an instance method. Try System.out.println(calendar.getTime().getTime()); in order to call the getTime() method on the Date representation of the Calendar, which is document to return the time in milliseconds between 1 January 1970 and the date represented by the Date object.

夜声 2024-10-11 23:54:01

对于问题的第二部分,您应该调用 Calendar.get(Calendar.MILLISECONDS) 来检索日历对象的毫秒字段。

For the second part of your question, you should call Calendar.get(Calendar.MILLISECONDS) to retrieve the milliseconds field of a calendar object.

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