I have time coming from gpslocation service in 1352437114052 format. Can some one tell me how to convert this into local time either in Java or Matlab or Excel.
-
Find out that the offset between local time and UTC is, in milliseconds, and add/subtract it from the value you have here. That gives local time in milliseconds since the beginning of 1970 in your timezone. – hmakholm left over Monica Nov 09 '12 at 20:49
-
1Similar Question, [GPS Time Representation library](http://stackoverflow.com/q/3038229/642706) and [GPS time in millis to UTC format](http://stackoverflow.com/q/9599176/642706). – Basil Bourque Sep 08 '15 at 05:03
6 Answers
Create a new Date
from your milliseconds since epoch. Then use a DateFormat
to format it in your desired timezone.
Date date = new Date(1352437114052L);
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getTimeZone("PST"));
System.out.println(format.format(date));

- 61,876
- 75
- 195
- 257
This is an epoch time and it represents Fri, 09 Nov 2012 04:58:34 GMT. This numeric value is an absolute point in time, irrespective to time zone.
If you want to see that point in time in different time zone, use GregorianCalendar
:
Calendar c = new GregorianCalendar(TimeZone.getTimeZone("EST"));
c.setTimeInMillis(1352437114052L);
c.get(Calendar.HOUR_OF_DAY); //20:58 the day before

- 334,321
- 69
- 703
- 674
-
The key is the use of `TimeZone`. Otherwise you get the default zone. – David R Tribble Nov 09 '12 at 20:54
-
The modern Java answer using the JVM’s time zone setting (typically the same as your computer’s time zone):
long time = 1_352_437_114_052L;
ZonedDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault());
System.out.println(dateTime);
Running on my computer I get
2012-11-09T05:58:34.052+01:00[Europe/Copenhagen]
To specify a time zone:
ZonedDateTime dateTime = Instant.ofEpochMilli(time).atZone(ZoneId.of("Asia/Almaty"));
2012-11-09T10:58:34.052+06:00[Asia/Almaty]
Question: Will that work on Android too?
To answer tinker’s comment here: Yes. I am using java.time
, the modern Java date and time API, and it works nicely on 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, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And 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). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
@Steve Kuo answered the question directly, almost. Here's a more general solution for machine's local time, including daylight saving time, where a
is of type BasicFileAttributes
as reported from Windows directory entry in public FileVisitResult visitFile(Path f, BasicFileAttributes a)
during Files.walkFileTree
:
String modifyDate;
Date date = new Date(a.lastModifiedTime().to(TimeUnit.MILLISECONDS));
DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
format.setTimeZone(TimeZone.getDefault());
modifyDate = (format.format(date)).substring(0,10);

- 4,234
- 4
- 53
- 88
long timeStamp = System.currentTimeMillis();
System.out.println(timeStamp+"");
Calendar calendar = Calendar.getInstance();
calendar.setTimeZone(TimeZone.getDefault());
calendar.setTimeInMillis(timeStamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a z");
String dateString = sdf.format(calendar.getTime());
System.out.println(dateString);
Output :
timestamp : 1528860439258
dateformat from sdf : 2018-06-12 08:27:19 PM PDT

- 2,352
- 3
- 21
- 43
-
1Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. – Ole V.V. Jun 13 '18 at 11:23
-
Instant.ofEpochMilli will not work on Android API level < 26. So, you need to tone it down. The question here is on Java, not specifically Android but there is a way to say it. – Jun 14 '18 at 21:27
-
@tinker Thanks for mentioning it. Indeed `Instant.ofEpochMilli` can work nicely on Android below API level 26. See [my edited answer](https://stackoverflow.com/a/50836151/5772882). – Ole V.V. Jun 17 '18 at 09:20
Since new Date(String string) is deprecated now(which is the accepted answer), we can use DateTimeZone.getDefault()
to get the system time zone
public String getZonedDate(String dateStr) {
DateTime utcDateTime = new DateTime(dateStr).toDateTime(DateTimeZone.UTC);
return utcDateTime
.toDateTime(DateTimeZone.getDefault()).toString("yyyy-MM-dd'T'HH:mm:ss");
}

- 712
- 6
- 22