简体   繁体   English

在 Java 中将 EST 转换为 EDT,反之亦然

[英]Converting EST to EDT or vice versa in Java

Can anybody tell about three things:谁能说说三件事:

  1. How to retrieve TimeZone from java.util.Date instance?如何从 java.util.Date 实例中检索 TimeZone?
  2. How to know whether Daylight savings is applicable?如何知道夏令时是否适用?
    I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000)我想我可以通过执行 timeZone.getDSTSavings 来知道它,但我面临的问题是,即使我将系统日期设为 2012 年 2 月 1 日,我仍然得到正值(我猜是 3600000)
  3. How to convert EST time to EDT or vice versa?如何将 EST 时间转换为 EDT,反之亦然?

How to retrieve TimeZone from java.util.Date instance?如何从 java.util.Date 实例中检索 TimeZone?

There's no such thing.没有这样的事情。 A Date just represents a number of milliseconds since the Unix epoch, which was midnight on January 1st 1970 UTC. Date表示自 Unix 纪元(UTC 1970 年 1 月 1 日午夜)以来的毫秒数。 It's not associated with a particular calendar system or time zone.它与特定的日历系统或时区无关。 To put it another way, if a friend and I are on the phone together (with a zero latency ;) and I click my fingers, we would both agree on the Date at which that click too place - even if I'm using the Gregorian calendar and he's using the Julian calendar, and even if I'm in London and he's in New York.换句话说,如果一个朋友和我一起打电话(零延迟;)并且我点击我的手指,我们都会同意点击的Date - 即使我正在使用公历他使用儒略历,即使我在伦敦而他在纽约。 It's the same instant in time .这是同一个瞬间

How to know whether Daylight savings is applicable?, I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000)如何知道夏令时是否适用?,我想我可以通过执行 timeZone.getDSTSavings 来了解它,但我面临的问题是,即使我将系统日期设为 2012 年 2 月 1 日,我仍然得到正值(我猜3600000)

Ideally, use Joda Time instead of java.util.Date / Calendar / TimeZone , but within TimeZone you can use TimeZone.getOffset(long) to find the offset from UTC, or TimeZone.inDaylightTime(Date) to just give you a yes/no answer.理想情况下,使用Joda Time而不是java.util.Date / Calendar / TimeZone ,但在TimeZone您可以使用TimeZone.getOffset(long)来查找与 UTC 的偏移量,或者TimeZone.inDaylightTime(Date)只是给您一个 yes/没有答案。

How to convert EST time to EDT or vice versa?如何将 EST 时间转换为 EDT,反之亦然?

Usually that's an invalid question - because at any one instance in time, either EST or EDT applies.通常这是一个无效的问题-因为在任何时间一个实例,无论是ESTEDT适用。 You normally convert from one time zone to another, and "EDT" and "EST" aren't different time zones - they're different offsets within the same time zone.您通常会从一个时区转换为另一个时区,而“EDT”和“EST”并不是不同的时区——它们是同一时区内的不同偏移量。 The fact that you're asking for this suggests that you may be modelling your data incorrectly to start with (which is unfortunately easy to do with date/time values).您要求这样做的事实表明您可能一开始就对数据进行了错误的建模(不幸的是,使用日期/时间值很容易做到这一点)。 Please give us more information and we may be able to help you more.请向我们提供更多信息,我们或许可以为您提供更多帮助。

  1. How to retrieve TimeZone from java.util.Date instance?如何从 java.util.Date 实例中检索 TimeZone?

You can view the Time Zone of an instantiated Date object by viewing it in toString() format.您可以通过以 toString() 格式查看实例化日期对象的时区。 Such as "Wed May 01 12:00:00 EDT 2013".例如“2013 年 5 月 1 日星期三 12:00:00 EDT”。

How to know whether Daylight savings is applicable?, I suppose I can know it by doing timeZone.getDSTSavings, but problem I am facing is that even if I make my system's date as Feb 1 2012, still I am getting the value as positive (I guess 3600000) 3. How to convert EST time to EDT or vice versa?如何知道夏令时是否适用?,我想我可以通过执行 timeZone.getDSTSavings 来了解它,但我面临的问题是,即使我将系统日期设为 2012 年 2 月 1 日,我仍然得到正值(我猜是 3600000) 3. 如何将 EST 时间转换为 EDT,反之亦然? This is critical question I have这是我的关键问题

The Date object will account for the EDT/EST automatically.日期对象将自动说明 EDT/EST。 For instance, if you have an instantiated Date object for November 28, then the toString() will include EST rather than EDT.例如,如果您有一个 11 月 28 日的实例化日期对象,那么 toString() 将包含 EST 而不是 EDT。

The problem I originally ran into was that I was trying to use EST for a calendar date where Daylight Savings Time was active, and the Date object was automatically correcting my mistake by adding an hour to my time and displaying EDT.我最初遇到的问题是我试图将 EST 用于夏令时处于活动状态的日历日期,而 Date 对象通过向我的时间添加一个小时并显示 EDT 来自动纠正我的错误。

I ran into a similar problem.我遇到了类似的问题。 The following solution is in Matlab but calling Java, and my changes are just comments because I'm working with Matlab datenum values.以下解决方案在 Matlab 中但调用 Java,我的更改只是注释,因为我正在使用 Matlab datenum 值。 You wouldn't really be able to convert timezone aware values, since they are the same timezone, but this may help with raw values that are not timezone aware.您实际上无法转换时区感知值,因为它们是相同的时区,但这可能有助于处理不感知时区的原始值。

%c1 GregorianCalendar of now
%c2 GregorianCalendar of date in question
tz1 = c1.getTimeZone; %Note t2 would be the same

in1 = tz1.inDaylightTime(c1.getTime);
in2 = tz1.inDaylightTime(c2.getTime);

if (in1 == in2)
    %do nothing
elseif in1
    %c1 EDT, c2 is EST
    %Subtract 1 hour to c2
else
    %c1 EST, c2 EDT
    %Add 1 hour to c2
end

Date stores the time in milliseconds since the 1970 epoch, so doesn't contain timezone information. Date 以毫秒为单位存储自 1970 纪元以来的时间,因此不包含时区信息。 Your JVM has a locale set which defines how this date is then displayed.您的 JVM 有一个语言环境集,用于定义此日期的显示方式。

If you want to find out what TimeZone your JVM has set, call 'TimeZone.getDefault()'如果您想了解 JVM 设置的时区,请调用“TimeZone.getDefault()”

Once you have a TimeZone object, you can call a number of functions to determine if for that timezone, daylight savings applies to a give date:拥有 TimeZone 对象后,您可以调用许多函数来确定该时区的夏令时是否适用于给定日期:

  • inDaylightTime(Date)白天时间(日期)

To (crudely) determine the difference between two TimeZones: (粗略地)确定两个时区之间的差异:

long edtOffset = TimeZone.getTimeZone("EDT").getOffset(date.getTime());
long estOffset = TimeZone.getTimeZone("EST").getOffset(date.getTime());
long diff = estOffset - edtOffset;

Although I'm not sure how valid this is as EST and EDT are mutually exclusive, and whether the above code will even give you the result you're interested in (I've not tested it)虽然我不确定这有多有效,因为 EST 和 EDT 是互斥的,而且上面的代码是否会给你你感兴趣的结果(我没有测试过)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM