[英]Java Calendar date manipulation issue
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. 但它显示为2009年3月4日星期三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? 我在这里失踪了什么?
The SimpleDateFormat#format(Object)
method returns a formatted String
, it does not affect how the Calendar#getTime()
method returns. SimpleDateFormat#format(Object)
方法返回格式化的String
,它不会影响Calendar#getTime()
方法返回的方式。 To that end you should do a: 为此你应该做一个:
System.out.println(format.format(calendar.getTime()));
The first problem that I see is that format.format(calendar.getTime());
我看到的第一个问题是format.format(calendar.getTime());
returns a String and doesn't actually change the formatting of the Calendar. 返回一个String,实际上并没有改变Calendar的格式。 You are going to want to replace the last two lines with System.out.println(format.format(calendar.getTime()));
您将要使用System.out.println(format.format(calendar.getTime()));
替换最后两行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). 使用您指定的格式实际打印出日期(假设您的SimpleDateFormat有效,但它看起来很快就能看到)。
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()
. 我不喜欢getTime()
和getTimeInMillis()
的Javadocs的措辞。 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". getTime()
方法返回“表示此Calendar
的时间值的Date
对象”,而getTimeInMillis()
返回“当前时间为UTC时间的毫秒”。 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. 对我来说,措辞的不同意味着getTimeInMillis()
不使用Calendar对象指定的日期,即使它是一个实例方法。 Try System.out.println(calendar.getTime().getTime());
尝试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. 为了在Calendar的Date
表示上调用getTime()
方法,该文档返回1970年1月1日到Date对象表示的日期之间的时间(以毫秒为单位)。
对于问题的第二部分,您应该调用Calendar.get(Calendar.MILLISECONDS)
来检索日历对象的毫秒字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.