Java日历日期操作问题
我尝试以以下格式获取当前日期/时间,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SimpleDateFormat#format(Object)
方法返回格式化的String
,它不会影响Calendar#getTime()
方法的返回方式。为此,您应该执行以下操作:The
SimpleDateFormat#format(Object)
method returns a formattedString
, it does not affect how theCalendar#getTime()
method returns. To that end you should do a:我看到的第一个问题是
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 withSystem.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()
andgetTimeInMillis()
. ThegetTime()
method returns "aDate
object representing thisCalendar
's time value" whilegetTimeInMillis()
returns "the current time as UTC milliseconds from the epoch". To me, the difference in wording implies thatgetTimeInMillis()
doesn't use the date specified by the Calendar object, even though it is an instance method. TrySystem.out.println(calendar.getTime().getTime());
in order to call thegetTime()
method on theDate
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.对于问题的第二部分,您应该调用
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.