JAVA UTC to EST from a long UTC timestamp

15,370

Solution 1

Try this:

Date estTime = new Date(utcTime.getTime() + TimeZone.getTimeZone("EST").getRawOffset());

Where utcTime is Date object of UTC time (if you already have the long value - just use it)

Solution 2

final Calendar c = Calendar.getInstance(TimeZone.getTimeZone("EST"));
c.setTimeInMillis(longTime);

Where longTime is the number of milliseconds since the epoch in UTC time. You can then use the methods of the Calendar class to get the various components of the date/time.

Solution 3

rd42, Can you give me a little more context on this?

You say you have a "UTC timestamp". Is this stored in a database? Is it a string?

I might be able to provide you more of an answer if you can give the context you're trying to work this in.


Ok for clarity's sake what you're saying is that you have a long value that represents a timestamp in UTC.

So in that case what you're going to want to do is the following.

import java.util.Calendar;
import java.util.TimeZone;

TimeZone utcTZ= TimeZone.getTimeZone("UTC");
Calendar utcCal= Calendar.getInstance(utcTZ);
utcCal.setTimeInMillis(utcAsLongValue);

Now you're calendar object is in UTC.

To display this though you're going to want to do something like the following:

import java.text.SimpleDateFormat;
import java.util.Date;

SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
sdf.setTimeZone(utcTZ);
Date utcDate= utcCal.getTime();
sdf.formatDate(utcDate);

This will allow you to read in a timestamp for the UTC time zone stored as a long value and convert it to a Java Calendar or Date object.

Hope that gets you where you need to be.

Share:
15,370
rd42
Author by

rd42

Updated on October 22, 2022

Comments

  • rd42
    rd42 over 1 year

    I'm trying to convert a long timestamp that is UTC to Eastern Standard Time and am totally lost. Any hints would be great!

    Thanks, R