4

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
ChanChow
  • 1,346
  • 7
  • 28
  • 57
  • 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
  • 1
    Similar 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 Answers6

5

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));
Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
4

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
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
1

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

catch23
  • 17,519
  • 42
  • 144
  • 217
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0

@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);
DSlomer64
  • 4,234
  • 4
  • 53
  • 88
0
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
cela
  • 2,352
  • 3
  • 21
  • 43
  • 1
    Please 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
0

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");
  }
Ganesh Jadhav
  • 712
  • 6
  • 22