如何在java中设置日期对象的时间
我用 Java 创建了一个 Date 对象。当我这样做时,它会显示如下内容:date=Tue Aug 09 00:00:00 IST 2011
。结果,我的Excel文件似乎减少了一天(2月27日变成2月26日等等)我想这一定是因为时间的原因。我怎样才能将其设置为下午 5:30 之类的时间?
I created a Date
object in Java. When I do so, it shows something like: date=Tue Aug 09 00:00:00 IST 2011
. As a result, it appears that my Excel file is lesser by one day (27 feb becomes 26 feb and so on) I think it must be because of time. How can I set it to something like 5:30 pm?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另请参阅
Also See
您可以显示用于设置日期对象的代码吗?无论如何<您可以使用此代码来初始化日期:
Can you show code which you use for setting date object? Anyway< you can use this code for intialisation of date:
我想贡献现代答案。这涉及使用现代 Java 日期和时间 API
java.time
,而不是旧的Date
或Calendar
,除非无法避免它。您的问题很可能确实是时区问题。当时间为 IST 2011 年 8 月 9 日星期二 00:00:00 时,在 IST 以西的时区,尚未到达午夜。现在仍然是 8 月 8 日。例如,如果您将日期输入 Excel 的 API 需要 UTC,则该日期将是您想要的日期的前一天。我相信真正好的解决方案是生成 00:00 UTC 的日期时间(或另一端预期并使用的任何时区或偏移量)。
此打印
Z
表示 UTC(将其视为距 UTC 或 Zulu 时区的零偏移量)。当然,如果您可以将LocalDate
从第一行代码传递到 Excel,那就更好了。它不包括一天中的时间,因此不会造成混淆。另一方面,如果您需要一个老式的Date
对象,请在传递Date
之前进行转换:在我的计算机上打印出来
不要被愚弄,这是正确的。我的时区(中欧时间)与 2 月(标准时间)UTC 的偏移量为 +01:00,因此这里的 01:00:00 等于 00:00:00 UTC。它只是
Date.toString()
获取 JVM 时区并使用它来生成字符串。要直接回答您的直接问题,如果您有
ZonedDateTime
、OffsetDateTime
或LocalDateTime
,在所有这些情况下,以下内容将完成您的要求for:如果
yourDateTime
是2018-02-27T00:00
的LocalDateTime
,那么现在将是2018-02-27T17: 30。类似地,对于其他类型,只有它们也适当地包括偏移量和时区。
如果您只有一个日期,如上面的第一个片段所示,您还可以向其中添加时间信息:不过,
对于大多数目的,您应该更愿意添加特定时区的时间信息,例如示例
这会产生
2018-02-27T17:30+05:30[Asia/Kolkata]
。Date
和Calendar
与java.time
您使用的
Date
类以及Calendar 和 SimpleDateFormat 早已过时,尤其是
SimpleDateFormat 已被证明很麻烦。在所有情况下,现代 Java 日期和时间 API 都更易于使用。这就是为什么我想为一个仍在被访问的老问题提供这个答案。
链接: Oracle 教程日期时间,解释如何使用
java.time
。I should like to contribute the modern answer. This involves using
java.time
, the modern Java date and time API, and not the oldDate
norCalendar
except where there’s no way to avoid it.Your issue is very likely really a timezone issue. When it is Tue Aug 09 00:00:00 IST 2011, in time zones west of IST midnight has not yet been reached. It is still Aug 8. If for example your API for putting the date into Excel expects UTC, the date will be the day before the one you intended. I believe the real and good solution is to produce a date-time of 00:00 UTC (or whatever time zone or offset is expected and used at the other end).
This prints
Z
means UTC (think of it as offset zero from UTC or Zulu time zone). Better still, of course, if you could pass theLocalDate
from the first code line to Excel. It doesn’t include time-of-day, so there is no confusion possible. On the other hand, if you need an old-fashionedDate
object for that, convert just before handing theDate
on:On my computer this prints
Don’t be fooled, it is correct. My time zone (Central European Time) is at offset +01:00 from UTC in February (standard time), so 01:00:00 here is equal to 00:00:00 UTC. It’s just
Date.toString()
grabbing the JVMs time zone and using it for producing the string.To answer your direct question directly, if you have a
ZonedDateTime
,OffsetDateTime
orLocalDateTime
, in all of these cases the following will accomplish what you asked for:If
yourDateTime
was aLocalDateTime
of2018-02-27T00:00
, it will now be2018-02-27T17:30
. Similarly for the other types, only they include offset and time zone too as appropriate.If you only had a date, as in the first snippet above, you can also add time-of-day information to it:
For most purposes you should prefer to add the time-of-day in a specific time zone, though, for example
This yields
2018-02-27T17:30+05:30[Asia/Kolkata]
.Date
andCalendar
vsjava.time
The
Date
class that you use as well asCalendar
andSimpleDateFormat
used in the other answers are long outdated, andSimpleDateFormat
in particular has proven troublesome. In all cases the modern Java date and time API is so much nicer to work with. Which is why I wanted to provide this answer to an old question that is still being visited.Link: Oracle Tutorial Date Time, explaining how to use
java.time
.如果您无权访问 java 8 和 API java.time,这是我使用旧的 java.util.Calendar 将一个日期的时间复制到另一个日期的简单函数(受 Jigar Joshi 启发):
If you don't have access to java 8 and the API java.time, here is my simple function to copy the time of one date to another date using the old java.util.Calendar (inspire by Jigar Joshi) :