相关疑难解决方法(0)

为什么减去这两次(在1927年)给出一个奇怪的结果?

如果我运行以下程序,它解析引用时间间隔为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-ld31(正如我所期望的那样,在时间上只有一秒钟的差异),但是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-ld31.


Java版本:

java version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04) …
Run Code Online (Sandbox Code Playgroud)

java timezone date

6628
推荐指数
9
解决办法
63万
查看次数

Javascript日期解析在Chrome中返回奇怪的结果

我在Chrome浏览器中发现了一些奇怪的Date行为(版本74.0.3729.131(官方内部版本)(64位))。在Chrome开发者控制台中执行了以下javascript:

new Date('1894-01-01T00:00:00+01:00')
// result: Mon Jan 01 1894 00:00:00 GMT+0100 (Central European Standard Time)

new Date('1893-01-01T00:00:00+01:00')
// result: Sat Dec 31 1892 23:53:28 GMT+0053 (Central European Standard Time)
Run Code Online (Sandbox Code Playgroud)

尽管已经提供了有效的ISO8601值,但我已经在不同的浏览器中通过Date ctor阅读了有关非标准日期解析的信息。但这不仅仅是奇怪的o_o

在Firefox(Quantum 66.0.3(64位))中,相同的调用会导致预期的Date对象:

new Date('1894-01-01T00:00:00+01:00')
// result: > Date 1892-12-31T23:00:00.000Z

new Date('1893-01-01T00:00:00+01:00')
// result: > Date 1893-12-31T23:00:00.000Z
Run Code Online (Sandbox Code Playgroud)
  • 这是Chrome中的错误吗?
  • 我的输入是有效的ISO8601,我猜呢?
  • 最重要的问题是,我该如何解决?(希望自己不解析输入字符串)

javascript google-chrome date

9
推荐指数
1
解决办法
156
查看次数

DateTimeFormatter和SimpleDateFormat产生不同的字符串

这不是某些人认为的重复。它是关于用于格式化日期的两个标准Java类,这些类在自时期以来以相同的毫秒值生成不同的字符串。

对于自1883年某个时间点之前的纪元以来的毫秒值,SimpleDateFormat和DateTimeFormatter将产生不同的结果。由于我不明白的原因,DateTimeFormatter会产生与我期望的字符串相差将近四分钟的字符串。

这很重要,因为我正在更改一些代码以使用DateTimeFormatter而不是SimpleDateFormat。我们的输入始终是从纪元以来的毫秒数,在更改代码后,我需要将值设置为相同。

先前的代码将以毫秒为单位创建一个Date,然后使用SimpleDateFormat对其进行格式化。

新代码从毫秒创建一个Instant,然后从Instant创建一个ZonedDateTime,然后创建一个DateTimeFormatter对其进行格式化。

这是我使用JUnit4和Hamcrest编写的测试。该测试查找自5月13日15:41:25的纪元以来的毫秒数,该时间为2019年开始的每一年,并且每次倒退一年。

对于每年,它使用SimpleDateFormat和DateTimeFormatter格式化毫秒格式,然后比较结果。

@Test
  public void testSimpleDateFormatVersusDateTimeFormatter() throws Exception {
    String formatString = "EEE MMM dd HH:mm:ss zzz yyyy";
    String timeZoneCode = "America/New_York";

    ZoneId zoneId = ZoneId.of(timeZoneCode);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatString);
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timeZoneCode));

    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatString);

    for (int year = 0; year < 200; year++) {

      long millis = getMillisSinceEpoch(2019 - year, 5, 13, 15, 41, 25, timeZoneCode);

      System.out.printf("%s%n", new Date(millis));

      // Format using a DateTimeFormatter;
      Instant instant = Instant.ofEpochMilli(millis);
      ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, zoneId); …
Run Code Online (Sandbox Code Playgroud)

java java.time

1
推荐指数
1
解决办法
61
查看次数

标签 统计

date ×2

java ×2

google-chrome ×1

java.time ×1

javascript ×1

timezone ×1