Is there any easy way to find the timestamp of a location while knowing the longitude and latitude of this certain location?
-
1A unix timestamp is not dependent on location. – Andy Turner Jan 24 '21 at 12:41
2 Answers
Is there any easy way to find the timestamp of a location while knowing the longitude and latitude of this certain location?
Yes, there is.
Instant instantNow = Instant.now();
long unixTimestamp = instantNow.getEpochSecond();
System.out.println("Current UNIX timestamp valid in all locations: "
+ unixTimestamp);
Output when running just now (in Europe/Copenhagen time zone):
Current UNIX timestamp valid in all locations: 1611544940
As Andy Turner said in the comment, a UNIX timestamp is independent of time zone. So no matter which location you are inquiring about, the above is all that you need.
I am using java.time, the modern Java date and time API.
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
- On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - Java 8+ APIs available through desugaring
- ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

- 71,965
- 6
- 74
- 110

- 81,772
- 15
- 137
- 161
-
1Ole V.V. - I have incorporated the one-liner question of the OP as the first line in your answer. I think it's important because tomorrow if the question is rephrased, the first line of your answer, `Yes, there is.` may not make sense until the viewer checks the edit history of the question. My apologies if my edit is not in the way you intend to keep your answer. – Arvind Kumar Avinash Jan 25 '21 at 20:08
As far as I know, you could get the timezone with external libraries or using some API like google's. Check this: https://developers.google.com/maps/documentation/timezone/overview

- 13
- 3