Java Date(日历)计算给定日期时间的本地时间一天的开始

发布于 2024-12-07 04:16:53 字数 1582 浏览 0 评论 0原文

所有,

在办公室度过了辛苦的一天...我正在尝试计算当地时间的一天开始时间,即午夜 00:00:00,给定任何日历日期。

给定接下来的几天,我想计算当地时间午夜的一天的开始:

       Time                     Start Day (Local)            Start Day (GMT)
2011-03-27 00:00:00 GMT -->     2011-03-27 00:00:00 GMT -->  2011-03-27 00:00:00 GMT
2011-03-27 01:00:00 GMT -->     2011-03-27 00:00:00 GMT -->  2011-03-27 00:00:00 GMT
2011-03-27 02:00:00 GMT -->     2011-03-27 00:00:00 GMT -->  2011-03-27 00:00:00 GMT
2011-04-01 00:00:00 BST -->     2011-04-01 00:00:00 BST -->  2011-03-31 23:00:00 GMT
2011-10-30 00:00:00 BST -->     2011-10-30 00:00:00 BST -->  2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 BST -->     2011-10-30 00:00:00 BST -->  2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 GMT -->     2011-10-30 00:00:00 BST -->  2011-10-29 23:00:00 GMT
2011-11-01 00:00:00 GMT -->     2011-11-01 00:00:00 GMT -->  2011-11-01 00:00:00 GMT

目前,我正在使用 SimpleDateFormat 将字符串时间解析为 GregorianCalendar。这给了我计算的 GMT/UTC 时间。

所以我有一些代码将字符串解析为 GregorianCalendar:

public GregorianCalendar getCalendar(String dateTime) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(sdf.parse(dateTime, new ParsePosition(0)));

    return cal;
}

现在我需要在本地将其设置为午夜:

public void setToStartOfDayLocally(GregorianCalendar cal) {
    ????
}

我不太确定此时需要使用日历做什么。不幸的是,我们不会在不久的将来转向 JODA 日期。我的例子也没有考虑不同的时区。

有什么想法吗?

谢谢,

安德斯

All,

Having a hard day in the office with this one... I am trying to workout the start of a day in Local Time, i.e. Midnight 00:00:00, given any calendar date.

Given the following days, I want to calculate the start of the day at local time midnight:

       Time                     Start Day (Local)            Start Day (GMT)
2011-03-27 00:00:00 GMT -->     2011-03-27 00:00:00 GMT -->  2011-03-27 00:00:00 GMT
2011-03-27 01:00:00 GMT -->     2011-03-27 00:00:00 GMT -->  2011-03-27 00:00:00 GMT
2011-03-27 02:00:00 GMT -->     2011-03-27 00:00:00 GMT -->  2011-03-27 00:00:00 GMT
2011-04-01 00:00:00 BST -->     2011-04-01 00:00:00 BST -->  2011-03-31 23:00:00 GMT
2011-10-30 00:00:00 BST -->     2011-10-30 00:00:00 BST -->  2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 BST -->     2011-10-30 00:00:00 BST -->  2011-10-29 23:00:00 GMT
2011-10-30 01:00:00 GMT -->     2011-10-30 00:00:00 BST -->  2011-10-29 23:00:00 GMT
2011-11-01 00:00:00 GMT -->     2011-11-01 00:00:00 GMT -->  2011-11-01 00:00:00 GMT

At present, I am parsing the String Time into a GregorianCalendar using a SimpleDateFormat. This gives me the GMT/UTC time to calculate from.

So I have some code which parses the string into a GregorianCalendar:

public GregorianCalendar getCalendar(String dateTime) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(sdf.parse(dateTime, new ParsePosition(0)));

    return cal;
}

Now I need to set it to midnight locally:

public void setToStartOfDayLocally(GregorianCalendar cal) {
    ????
}

I am not too sure what I need to do at the minute with the Calendar. Unfortunately we are not moving to JODA date in the near future. I am also not accounting for different time zones with my example.

Any thoughts?

Thanks,

Andez

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

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

发布评论

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

评论(3

深海蓝天 2024-12-14 04:16:53

我认为这段代码可以解决您的问题。我正在使用它从本地时区的时间转换为另一个时区的时间。

public static Date convertLocalDateToDateTimezone( Date localDate, String timezone ) {
   TimeZone localTimeZone = TimeZone.getDefault();
   TimeZone timezone = TimeZone.getTimeZone( timezone );
   long gmtMillis = localDate.getTime();
   long result = gmtMillis + timezone.getOffset( gmtMillis ) - localTimeZone.getOffset(     gmtMillis );
   return new Date( result );
}

希望这有帮助。

I think this code might solve your issue. I am using it to convert from time in local timezone to another.

public static Date convertLocalDateToDateTimezone( Date localDate, String timezone ) {
   TimeZone localTimeZone = TimeZone.getDefault();
   TimeZone timezone = TimeZone.getTimeZone( timezone );
   long gmtMillis = localDate.getTime();
   long result = gmtMillis + timezone.getOffset( gmtMillis ) - localTimeZone.getOffset(     gmtMillis );
   return new Date( result );
}

Hope this helps.

離殇 2024-12-14 04:16:53

听起来您只想将所有时间部分设置为零。您可以使用:(

cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

假设您已正确设置日历中的时区。)

请注意,这不一定有效 - 在某些时区(例如巴西),在夏令时午夜过渡要么发生两次,要么根本不发生。

就我个人而言,我会开始发出关于搬到 Joda Time 的声音,这使得所有这一切变得更加简单:)

It sounds like you just want to set all of the time parts to zero. You can use:

cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);

(Assuming you've got the time zone in the calendar set properly.)

Note that that isn't necessarily valid though - in some time zones (e.g. Brazil), at a daylight saving transition midnight either occurs twice or not at all.

Personally I'd start making noises about moving to Joda Time though, which makes all of this a lot simpler :)

掩饰不了的爱 2024-12-14 04:16:53

java.time

您正在使用过时的类。在 Java 8 及更高版本中,使用内置 java .time 框架。

不要假设一天的开始时间为 00:00:00.0。由于夏令时 (DST) 等异常情况,一天的开始时间可能会因时区而异。让 java.time 类通过调用 LocalDate::atStartOfDay

避免使用 3-4 个字母的区域缩写,例如 BSTEST 等。它们既不是标准化的也不是独特的。使用正确的时区名称

即时UTC 时间线上的一个时刻。应用时区来获取 ZonedDateTime。从该日期时间中提取仅日期值, 本地日期。通过调用 atStartOfDay 返回到 ZonedDateTime 设置为该时区当天的第一时刻。

Instant now = Instant.now();
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
LocalDate localDate = zdt.toLocalDate();
ZonedDateTime zdtStart = localDate.atStartOfDay( zoneId );

提示:避免使用“午夜”这个词。没有一个准确的定义。有些人想到尝试确定一天的最后时刻,这是一个问题,因为秒的分数是无限可分的。有些人认为特殊字符串“24:00”表示午夜的钟声,这进一步混淆了日期时间处理。 Joda-Time 项目的经验告诉我们,最好将“一天中的第一时刻”作为一个清晰而准确的含义。另外,请注意,由于夏令时 (DST) 和某些时区的其他异常情况,第一时刻并不总是 00:00:00。这就是为什么我们依赖于 atStartOfDay 方法而不是硬编码一天中的零时间。

java.time

You are using outmoded classes. In Java 8 and later, use built-in java.time framework.

Do not assume the day starts at the time 00:00:00.0. Because of anomalies such as Daylight Saving Time (DST) the start-of-day may vary by time zone. Let the java.time classes determine the correct time with a call to LocalDate::atStartOfDay.

Avoid using 3-4 letter zone abbreviations such as BST, EST, and so on. They are neither standardized nor unique. Use proper time zone names.

An Instant is a moment on the timeline in UTC. Apply a time zone to get a ZonedDateTime. Extract a date-only value from that date-time, a LocalDate. Apply a time zone with a call to atStartOfDay to get back to a ZonedDateTime set to the first moment of the day in that time zone.

Instant now = Instant.now();
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
LocalDate localDate = zdt.toLocalDate();
ZonedDateTime zdtStart = localDate.atStartOfDay( zoneId );

Tip: Avoid the word “midnight”. Does not have a precise definition. Some people think of trying to determine the last moment of the day which is a problem because of an infinitely divisible fraction of a second. Some people think of the the special string “24:00” to indicate the stroke of midnight which further confuses date-time handling. Experience from the Joda-Time project teaches us that it is best to focus on “first moment of the day” as a clear and precise meaning. Also, be aware that the first moment is not always 00:00:00, due to Daylight Saving Time (DST) and other anomalies in some time zones. That is why we depend on the atStartOfDay method rather than hard-code a zero-time-of-day.

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