Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert UTC time into local time in Java?

Tags:

java

time

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.

like image 219
ChanChow Avatar asked Dec 26 '22 14:12

ChanChow


2 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));
like image 159
Steve Kuo Avatar answered Jan 08 '23 01:01

Steve Kuo


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
like image 33
Tomasz Nurkiewicz Avatar answered Jan 08 '23 02:01

Tomasz Nurkiewicz