ChanChow
ChanChow

Reputation: 1366

How to convert UTC time into local time in Java?

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.

Upvotes: 4

Views: 18016

Answers (6)

Ganesh Jadhav
Ganesh Jadhav

Reputation: 802

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");
  }

Upvotes: 0

Anonymous
Anonymous

Reputation: 86359

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

Upvotes: 1

user8668058
user8668058

Reputation:

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

Upvotes: 0

DSlomer64
DSlomer64

Reputation: 4283

@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);

Upvotes: 0

Steve Kuo
Steve Kuo

Reputation: 63134

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

Upvotes: 5

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340933

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

Upvotes: 4

Related Questions