如何在java中设置日期对象的时间

发布于 2024-10-19 15:24:21 字数 165 浏览 3 评论 0原文

我用 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 技术交流群。

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

发布评论

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

评论(5

回忆躺在深渊里 2024-10-26 15:24:21
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();

另请参阅

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,17);
cal.set(Calendar.MINUTE,30);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MILLISECOND,0);

Date d = cal.getTime();

Also See

你的背包 2024-10-26 15:24:21

您可以显示用于设置日期对象的代码吗?无论如何<您可以使用此代码来初始化日期:

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2011-01-01 00:00:00")

Can you show code which you use for setting date object? Anyway< you can use this code for intialisation of date:

new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2011-01-01 00:00:00")
爱格式化 2024-10-26 15:24:21

我想贡献现代答案。这涉及使用现代 Java 日期和时间 API java.time,而不是旧的 DateCalendar,除非无法避免它。

您的问题很可能确实是时区问题。当时间为 IST 2011 年 8 月 9 日星期二 00:00:00 时,在 IST 以西的时区,尚未到达午夜。现在仍然是 8 月 8 日。例如,如果您将日期输入 Excel 的 API 需要 UTC,则该日期将是您想要的日期的前一天。我相信真正好的解决方案是生成 00:00 UTC 的日期时间(或另一端预期并使用的任何时区或偏移量)。

    LocalDate yourDate = LocalDate.of(2018, Month.FEBRUARY, 27);
    ZonedDateTime utcDateDime = yourDate.atStartOfDay(ZoneOffset.UTC);
    System.out.println(utcDateDime);

此打印

2018-02-27T00:00Z

Z 表示 UTC(将其视为距 UTC 或 Zulu 时区的零偏移量)。当然,如果您可以将 LocalDate 从第一行代码传递到 Excel,那就更好了。它不包括一天中的时间,因此不会造成混淆。另一方面,如果您需要一个老式的 Date 对象,请在传递 Date 之前进行转换:

    Date oldfashionedDate = Date.from(utcDateDime.toInstant());
    System.out.println(oldfashionedDate);

在我的计算机上打印出来

Tue Feb 27 01:00:00 CET 2018

不要被愚弄,这是正确的。我的时区(中欧时间)与 2 月(标准时间)UTC 的偏移量为 +01:00,因此这里的 01:00:00 等于 00:00:00 UTC。它只是 Date.toString() 获取 JVM 时区并使用它来生成字符串。

如何将其设置为下午 5:30 之类的时间?

要直接回答您的直接问题,如果您有 ZonedDateTimeOffsetDateTimeLocalDateTime,在所有这些情况下,以下内容将完成您的要求for:

    yourDateTime = yourDateTime.with(LocalTime.of(17, 30));

如果 yourDateTime2018-02-27T00:00LocalDateTime,那么现在将是 2018-02-27T17: 30。类似地,对于其他类型,只有它们也适当地包括偏移量和时区。

如果您只有一个日期,如上面的第一个片段所示,您还可以向其中添加时间信息:不过,

    LocalDate yourDate = LocalDate.of(2018, Month.FEBRUARY, 27);
    LocalDateTime dateTime = yourDate.atTime(LocalTime.of(17, 30));

对于大多数目的,您应该更愿意添加特定时区的时间信息,例如示例

    ZonedDateTime dateTime = yourDate.atTime(LocalTime.of(17, 30))
            .atZone(ZoneId.of("Asia/Kolkata"));

这会产生 2018-02-27T17:30+05:30[Asia/Kolkata]

DateCalendarjava.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 old Date nor Calendar 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).

    LocalDate yourDate = LocalDate.of(2018, Month.FEBRUARY, 27);
    ZonedDateTime utcDateDime = yourDate.atStartOfDay(ZoneOffset.UTC);
    System.out.println(utcDateDime);

This prints

2018-02-27T00:00Z

Z means UTC (think of it as offset zero from UTC or Zulu time zone). Better still, of course, if you could pass the LocalDate 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-fashioned Date object for that, convert just before handing the Date on:

    Date oldfashionedDate = Date.from(utcDateDime.toInstant());
    System.out.println(oldfashionedDate);

On my computer this prints

Tue Feb 27 01:00:00 CET 2018

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.

How can I set it to something like 5:30 pm?

To answer your direct question directly, if you have a ZonedDateTime, OffsetDateTime or LocalDateTime, in all of these cases the following will accomplish what you asked for:

    yourDateTime = yourDateTime.with(LocalTime.of(17, 30));

If yourDateTime was a LocalDateTime of 2018-02-27T00:00, it will now be 2018-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:

    LocalDate yourDate = LocalDate.of(2018, Month.FEBRUARY, 27);
    LocalDateTime dateTime = yourDate.atTime(LocalTime.of(17, 30));

For most purposes you should prefer to add the time-of-day in a specific time zone, though, for example

    ZonedDateTime dateTime = yourDate.atTime(LocalTime.of(17, 30))
            .atZone(ZoneId.of("Asia/Kolkata"));

This yields 2018-02-27T17:30+05:30[Asia/Kolkata].

Date and Calendar vs java.time

The Date class that you use as well as Calendar and SimpleDateFormat used in the other answers are long outdated, and SimpleDateFormat 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.

九公里浅绿 2024-10-26 15:24:21

如果您无权访问 java 8 和 API java.time,这是我使用旧的 java.util.Calendar 将一个日期的时间复制到另一个日期的简单函数(受 Jigar Joshi 启发):

/**
 * Copy only the time of one date to the date of another date.
 */
public static Date copyTimeToDate(Date date, Date time) {
    Calendar t = Calendar.getInstance();
    t.setTime(time);

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, t.get(Calendar.HOUR_OF_DAY));
    c.set(Calendar.MINUTE, t.get(Calendar.MINUTE));
    c.set(Calendar.SECOND, t.get(Calendar.SECOND));
    c.set(Calendar.MILLISECOND, t.get(Calendar.MILLISECOND));
    return c.getTime();
}

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) :

/**
 * Copy only the time of one date to the date of another date.
 */
public static Date copyTimeToDate(Date date, Date time) {
    Calendar t = Calendar.getInstance();
    t.setTime(time);

    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.set(Calendar.HOUR_OF_DAY, t.get(Calendar.HOUR_OF_DAY));
    c.set(Calendar.MINUTE, t.get(Calendar.MINUTE));
    c.set(Calendar.SECOND, t.get(Calendar.SECOND));
    c.set(Calendar.MILLISECOND, t.get(Calendar.MILLISECOND));
    return c.getTime();
}
她如夕阳 2024-10-26 15:24:21
Calendar calendar = new Calendar.Builder()
                .setDate(2022, Calendar.JUNE, 1)
                .setTimeOfDay(0, 0, 0)
                .build();

System.out.println(calendar.getTimeInMillis());
Calendar calendar = new Calendar.Builder()
                .setDate(2022, Calendar.JUNE, 1)
                .setTimeOfDay(0, 0, 0)
                .build();

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