Explaination of time format (Google Directions API)

我们两清 提交于 2020-06-18 14:02:47

问题


I have read the documentation of the Google Directions API for making a direction request. An example of a URL is given as

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

The value of the departure_time variable is supposed to reflect the following information:

July 30, 2012 at 09:45 am.

Can someone please explain this time format.

Thanks.


回答1:


It's a timestamp - seconds elapsed since the Unix epoch, 1970-01-01 00:00:00 UTC. If you want "right now" in that format, you can use System.currentTimeMillis() / 1000, or if you have a Date object, you can use date.getTime() / 1000.




回答2:


That's an epoch unix timestamp (number of seconds since Jan 1 1970). You can create a date by

Date d = new Date(1343605500L);

Or use http://www.epochconverter.com/




回答3:


Flaw In Google Documentation

Googling for that particular number led to places such as this similar StackOverflow.com question. These pages lead me to conclude that the documentation for Google Directions API is flawed.

You and others report that the doc says 1343605500 = July 30, 2012 at 09:45 am in New York. But that is incorrect. Both the day of month and the hour of day are wrong.

1343605500 seconds from the beginning of the year 1970 UTC/GMT:

  • In New York is 2012-07-29T19:45:00.000-04:00
  • In UTC/GMT is 2012-07-29T23:45:00.000Z

Getting Date-Time From A Number

As the other answers stated, apparently Google is handing you the number of seconds since the Unix Epoch at the beginning of the year 1970 in UTC/GMT (no time zone offset).

Alternatively to using java.util.Date/Calendar classes, you can use the third-party open-source Joda-Time library.

Here is some example source code to show you how to parse the text into a date-time with time zone.

// © 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 ) );

When run…

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

When using a count from epoch, you must be careful about:

  • Which epoch (Unix Time is but one of several possibilities)
  • Precision of count (seconds, milliseconds, nanoseconds)

Diagram of precision of time since epoch, with POSIX Unix time using seconds, java.util.Date and Joda-time using milliseconds, and JSR 310 java.time.* classes using nanoseconds



来源:https://stackoverflow.com/questions/15620486/explaination-of-time-format-google-directions-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!