如果我运行以下程序,它解析引用时间间隔为1秒的两个日期字符串并比较它们:
public static void main(String[] args) throws ParseException {
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str3 = "1927-12-31 23:54:07";
String str4 = "1927-12-31 23:54:08";
Date sDt3 = sf.parse(str3);
Date sDt4 = sf.parse(str4);
long ld3 = sDt3.getTime() /1000;
long ld4 = sDt4.getTime() /1000;
System.out.println(ld4-ld3);
}
Run Code Online (Sandbox Code Playgroud)
输出是:
353
为什么ld4-ld3不1(正如我所期望的那样,在时间上只有一秒钟的差异),但是353?
如果我将日期更改为1秒后的时间:
String str3 = "1927-12-31 23:54:08";
String str4 = "1927-12-31 23:54:09";
Run Code Online (Sandbox Code Playgroud)
然后ld4-ld3会1.
Java版本:
java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04) …Run Code Online (Sandbox Code Playgroud) 我希望能够将这个问题及其答案作为处理夏令时的权威指南,特别是处理实际的变更问题.
如果您有任何要添加的内容,请执行此操作
许多系统依赖于保持准确的时间,问题在于由于夏令时改变时间 - 向前或向后移动时钟.
例如,在订单获取系统中有一个业务规则取决于订单的时间 - 如果时钟发生变化,规则可能不那么明确.如何保持订单的时间?当然有无数的场景 - 这只是一个说明性的场景.
同样重要的是,如果不是这样的话:
我会对编程,操作系统,数据持久性和该问题的其他相关方面感兴趣.
一般答案很好,但我也希望看到细节,特别是如果它们只在一个平台上可用.
目前,我们有一种以TimeZone感知方式处理.net DateTimes的标准方法:每当我们生成一个DateTimeUTC时(例如使用DateTime.UtcNow),每当我们显示一个时,我们就会从UTC转换回用户的本地时间.
这工作正常,但我一直在阅读DateTimeOffset它如何捕获对象本身的本地和UTC时间.所以问题是,使用DateTimeOffsetvs我们已经做的事情有什么好处?
Web服务器是否有标准方法可以确定用户在网页中的时区?
也许来自HTTP标头或user-agent字符串的一部分?
我如何收集访客的时区信息?我需要Timezone,以及GMT抵消时间.
time.time()Python时间模块中是否返回系统的时间或UTC时间?
当我创建一个新Date对象时,它会初始化为当前时间但在本地时区.如何获得GMT中的当前日期和时间?
我需要做什么
我有一个时区不知道的日期时间对象,我需要添加一个时区,以便能够将其与其他时区感知日期时间对象进行比较.我不想将我的整个应用程序转换为时区,而不是因为这个遗留案例.
我试过的
首先,要证明问题:
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import pytz
>>> unaware = datetime.datetime(2011,8,15,8,15,12,0)
>>> unaware
datetime.datetime(2011, 8, 15, 8, 15, 12)
>>> aware = datetime.datetime(2011,8,15,8,15,12,0,pytz.UTC)
>>> aware
datetime.datetime(2011, 8, 15, 8, 15, 12, tzinfo=<UTC>)
>>> aware == unaware
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't compare offset-naive and offset-aware …Run Code Online (Sandbox Code Playgroud) 我有一个网页,每天,每月和每年有三个下拉菜单.如果我使用Date带有数字的JavaScript 构造函数,那么我得到一个Date当前时区的对象:
new Date(xiYear, xiMonth, xiDate)
Run Code Online (Sandbox Code Playgroud)
给出正确的日期,但由于夏令时,它认为日期是GMT + 01:00.
这里的问题是我然后将它传递Date给Ajax方法,当日期在服务器上反序列化时,它已经转换为GMT,因此丢失了一个小时,将一天移回一.现在我可以将日,月和年单独传递到Ajax方法中,但似乎应该有更好的方法.
接受的答案指出了我正确的方向,但只是单独使用setUTCHours()改变了:
Apr 5th 00:00 GMT+01:00
Run Code Online (Sandbox Code Playgroud)
至
Apr 4th 23:00 GMT+01:00
Run Code Online (Sandbox Code Playgroud)
然后我还必须设置UTC日期,月份和年份以结束
Apr 5th 01:00 GMT+01:00
Run Code Online (Sandbox Code Playgroud)
这就是我想要的.
当我请求在服务器上将PHP版本从5.2.17 更新到PHP 5.3.21 时,我收到此错误.
<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: date(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EDT/-4.0/DST' instead</p>
<p>Filename: libraries/Log.php</p>
<p>Line Number: 86</p>
</div>
Warning: …Run Code Online (Sandbox Code Playgroud)