【问题标题】:Explaination of time format (Google Directions API)时间格式说明(Google Directions API)
【发布时间】:2026-01-25 16:00:01
【问题描述】:

我已阅读 Google Directions API 的文档以提出方向请求。一个 URL 的例子是

http://maps.googleapis.com/maps/api/directions/json?origin=*lyn&destination=Queens&sensor=false&departure_time=1343605500&mode=transit

departure_time 变量的值应该反映以下信息:

2012 年 7 月 30 日上午 9:45。

谁能解释一下这个时间格式。

谢谢。

【问题讨论】:

  • 您能链接到相关文档吗?该文档可能存在缺陷。

标签: java android google-maps time google-directions-api


【解决方案1】:

这是一个时间戳 - 自 Unix epoch、1970-01-01 00:00:00 UTC 以来经过的秒数。如果你想要那种格式的“现在”,你可以使用System.currentTimeMillis() / 1000,或者如果你有一个Date对象,你可以使用date.getTime() / 1000

【讨论】:

  • @RaghavSood 实际上不是。时代的 1343605500 毫秒是 1970-01-16 13:13:25 UTC。
  • @gustafc 虽然这个答案在概念上是正确的,但实际上是错误的。自 Unix 纪元以来的 1343605500 秒的出发时间等于 2012 年 7 月 30 日上午 9:45 在纽约或 UTC。见我的answer。如果我错了,请纠正我。
  • @BasilBourque 五年后我才看到你的评论。我做了一些调查,我必须纠正你:) 你说得对,时间戳是 2012-07-30 09:45,既不是 UTC 时间也不是纽约时间,但文档没有提到时区。一些实验表明时间戳正好是2012-07-30T09:45+10:00,但是为什么要使用UTC+10呢?根据*,后来成为 Google Maps 的公司位于悉尼,使用 AEST 时区,即……UTC+10!
【解决方案2】:

这是一个纪元 unix 时间戳(自 1970 年 1 月 1 日以来的秒数)。您可以通过

创建日期
Date d = new Date(1343605500L);

或使用http://www.epochconverter.com/

【讨论】:

    【解决方案3】:

    Google 文档中的缺陷

    在谷歌上搜索该特定数字会导致诸如this similar *.com question 之类的地方。这些页面让我得出结论,Google Directions API 的文档存在缺陷。

    您和其他人报告说,该文档说 1343605500 = 2012 年 7 月 30 日上午 9:45 在纽约。但这是不正确的。月份的日期和时间都是错误的。

    1343605500 距 1970 年 UTC/GMT 年初的秒数:

    • 在纽约是2012-07-29T19:45:00.000-04:00
    • 在 UTC/GMT 中是 2012-07-29T23:45:00.000Z

    从数字中获取日期时间

    正如其他答案所述,显然 Google 正在向您提供自 1970 年初 UTC/GMT 的 Unix 纪元以来的秒数(无时区偏移)。

    除了使用 java.util.Date/Calendar 类之外,您还可以使用第三方开源 Joda-Time 库。

    这里是一些示例源代码,向您展示如何将文本解析为带时区的日期时间。

    // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
    // import org.joda.time.*;
    // import org.joda.time.format.*;
    
    // Starting data.
    String string = "1343605500";
    String timeZoneName = "America/New_York";
    
    // Convert string of seconds to number of milliseconds.
    long millis = Long.parseLong( string ) * 1000 ; //
    // Specify time zone rather than rely on default.
    DateTimeZone timeZone = DateTimeZone.forID( timeZoneName );
    // Instantiate DateTime object.
    DateTime dateTime = new DateTime( millis, timeZone );
    
    System.out.println( "dateTime: " + dateTime );
    System.out.println( "dateTime in UTC/GMT: " + dateTime.toDateTime( DateTimeZone.UTC ) );
    

    运行时……

    dateTime: 2012-07-29T19:45:00.000-04:00
    dateTime in UTC/GMT: 2012-07-29T23:45:00.000Z
    

    使用 epoch 计数时,必须注意:

    • 哪个纪元(Unix 时间只是几种可能性之一)
    • 计数精度(秒、毫秒、纳秒)

    【讨论】: