Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC Date to local Date

I am converting from epoch time (which is in UTC) to a format as shown below. Now I tried different SO answers to convert UTCDate from UTC to local time. But I am not getting the local time.

Any help would be appreciated.

String epochTime = "1436831775043";

Date UTCDate = new Date(Long.parseLong(epochTime));
Date localDate; // How to get this?

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("h:mm a");
String result = simpleDateFormat.format(UTCDate);

Also, the conversion has to be done without the help of any external library.

like image 349
Kevin Richards Avatar asked Jan 08 '23 23:01

Kevin Richards


2 Answers

Java 8

String epochTime = "1436831775043";

Instant utcInstant = new Date(Long.parseLong(epochTime)).toInstant();
ZonedDateTime there = ZonedDateTime.ofInstant(utcInstant, ZoneId.of("UTC"));

System.out.println(utcInstant);
LocalDateTime here = there.withZoneSameInstant(ZoneId.systemDefault()).toLocalDateTime();
System.out.println(here);

Which outputs:

2015-07-13T23:56:15.043Z
2015-07-14T09:56:15.043

After thoughts...

I think you're chasing your tail. Date is just a container for the number of milliseconds since the epoch (January 1, 1970, 00:00:00 GMT). It doesn't internally carry a representation of a time zone (AFAIK).

For example...

String epochTime = "1436831775043";
Date UTCDate = new Date(Long.parseLong(epochTime));
// Prints the "representation" of the Date
System.out.println(UTCDate);

// Local date/time format...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm:ss a");
try {
    System.out.println("local format: " + simpleDateFormat.format(UTCDate));
    System.out.println("local Date: " + simpleDateFormat.parse(simpleDateFormat.format(UTCDate)));
} catch (ParseException ex) {
    Logger.getLogger(JavaApplication203.class.getName()).log(Level.SEVERE, null, ex);
}

// UTC date/time format
try {
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    System.out.println("utc format: " + simpleDateFormat.format(UTCDate));
    System.out.println("utc date: " + simpleDateFormat.parse(simpleDateFormat.format(UTCDate)));
} catch (ParseException ex) {
    Logger.getLogger(JavaApplication203.class.getName()).log(Level.SEVERE, null, ex);
}

Which outputs...

Tue Jul 14 09:56:15 EST 2015
local format: 14/07/2015 9:56:15 AM
local Date: Tue Jul 14 09:56:15 EST 2015
utc format: 13/07/2015 11:56:15 PM
utc date: Tue Jul 14 09:56:15 EST 2015

If you have a look at local Date and utc date they are the same thing, even though the local format and utc format are formatted correctly.

So, instead of chasing your tale trying to get Date to "represent" a value you want, either use Java 8's Time API or JodaTime to manage the Time Zone information or simply format the Date into the Time Zone you want...

Further, if we do something like...

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy h:mm:ss a");
Date localDate = simpleDateFormat.parse(simpleDateFormat.format(UTCDate));
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date utcDate = simpleDateFormat.parse(simpleDateFormat.format(UTCDate));

System.out.println(localDate.getTime());
System.out.println(utcDate.getTime());

System.out.println(localDate.equals(utcDate));

It will print...

1436831775000
1436831775000
true
like image 55
MadProgrammer Avatar answered Jan 16 '23 13:01

MadProgrammer


You can set your time zone in the formatter:

simpleDateFormat.setTimeZone(TimeZone.getDefault());
like image 24
Nitesh Patel Avatar answered Jan 16 '23 13:01

Nitesh Patel