Date d = Calendar.getTime() 返回错误的时间?

发布于 2024-12-07 22:32:29 字数 302 浏览 0 评论 0原文

Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR, 8);
calendar.set(calendar.MINUTE, 45);
calendar.set(calendar.SECOND, 00);
    
Date d = calendar.getTime();
    
System.out.println(d);

输出是: 2011 年 10 月 5 日星期三 20:45:00 BST

任何人都可以帮我解释为什么会这样吗?

Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR, 8);
calendar.set(calendar.MINUTE, 45);
calendar.set(calendar.SECOND, 00);
    
Date d = calendar.getTime();
    
System.out.println(d);

Output is:
Wed Oct 05 20:45:00 BST 2011

Can anyone help me with why this is?

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

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

发布评论

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

评论(3

强者自强 2024-12-14 22:32:30

Calendar.HOUR< /a> 用于设置 12 小时制的小时。

换句话说,您确实日历设置为20:45。现在是晚上 8:45。

使用 Calendar.HOUR_OF_DAY 设置 24 小时制值。

Calendar.HOUR is used to set the hour as set on a 12-hour clock.

In other words, you do set the Calendar to 20:45. It's 8:45 PM.

Use Calendar.HOUR_OF_DAY to set the 24-hour-clock value.

jJeQQOZ5 2024-12-14 22:32:30

当然 - 您正在设置 日历。 HOUR 代表 1-12“半天的小时”。您应该使用 HOUR_OF_DAY 这是全天的 0-23 值:

calendar.set(Calendar.HOUR_OF_DAY, 8);

或者,使用 Joda Time是更好的日期/时间 API :)

顺便说一句,不要通过引用引用静态成员...当您调用 someOtherThread.sleep(.. .) 并且你当前的线程处于休眠状态...

Sure - you're setting Calendar.HOUR which represents the 1-12 "hour of half day". You should be using HOUR_OF_DAY which is the 0-23 value for the whole day:

calendar.set(Calendar.HOUR_OF_DAY, 8);

Alternatively, use Joda Time which is a much nicer date/time API :)

As an aside, please don't refer to static members via references... it will lead to pain when you call someOtherThread.sleep(...) and your current thread sleeps...

蓬勃野心 2024-12-14 22:32:30

java.time

java.util Date-Time API 及其格式化 API SimpleDateFormat 已过时且容易出错。建议完全停止使用它们并切换到 现代日期时间 API*

使用现代日期时间 API java.time 的解决方案:

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        Instant instant = OffsetDateTime.now(ZoneOffset.UTC)
                            .withHour(8)
                            .withMinute(45)
                            .withSecond(0)
                            .toInstant();
        System.out.println(instant);
    }
}

< kbd>在线演示

Instant 表示 UTC。输出中的 Z时区指示符零时区偏移。它代表 Zulu,指定 Etc/UTC 时区(时区偏移量为 +00:00 小时)。

无论出于何种原因,如果您需要将 Instant 对象转换为 java.util.Date 对象,可以按如下方式进行:

Date date = Date.from(instant);

注意:< /strong> 最有可能的是,您也希望秒的分数为零。如果是,只需在现有代码中添加 .withNano(0) 即可,如下所示:

OffsetDateTime.now(ZoneOffset.UTC)
    .withHour(8)
    .withMinute(45)
    .withSecond(0)
    .withNano(0)
    .toInstant();

跟踪:日期时间


* 无论出于何种原因,如果您必须坚持使用 Java 6 或 Java 7,您可以使用 ThreeTen-Backport 将大部分 java.time 功能向后移植到 Java 6 和 Java 6 7. 如果您正在处理 Android 项目,并且您的 Android API 级别仍然不符合 Java-8,请检查 通过脱糖提供 Java 8+ API如何在Android项目中使用ThreeTenABP

java.time

The java.util Date-Time API and their formatting API, SimpleDateFormat are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.

Solution using java.time, the modern Date-Time API:

import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class Main {
    public static void main(String[] args) {
        Instant instant = OffsetDateTime.now(ZoneOffset.UTC)
                            .withHour(8)
                            .withMinute(45)
                            .withSecond(0)
                            .toInstant();
        System.out.println(instant);
    }
}

ONLINE DEMO

An Instant represents an instantaneous point on the timeline in UTC. The Z in the output is the timezone designator for a zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

For any reason, if you need to convert this object of Instant to an object of java.util.Date, you can do so as follows:

Date date = Date.from(instant);

Note: Most likely, you would like the fraction-of-second too to be zero. If yes, just add .withNano(0) in the existing code as shown below:

OffsetDateTime.now(ZoneOffset.UTC)
    .withHour(8)
    .withMinute(45)
    .withSecond(0)
    .withNano(0)
    .toInstant();

Learn more about the modern Date-Time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

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