开发者

JAVA UTC to EST from a long UTC timestamp

开发者 https://www.devze.com 2022-12-27 03:34 出处:网络
I\'m trying to convert a long timestamp that is UTC to Eastern Standard Time and am totally lost.Any hints would be great!

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


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)


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.


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.


Solution using, java.time the modern API

An Instant represents an instantaneous point on the timeline. It is independent of a timezone. In order to represent it in a timezone, you can use Instant#atZone or ZonedDateTime#ofInstant as shown below:

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(String[] args) {
        // A sample timestamp
        long millis = 1620999618896L;

        Instant instant = Instant.ofEpochMilli(millis);
        System.out.println(instant);

        ZonedDateTime zdtET = instant.atZone(ZoneId.of("America/New_York"));
        System.out.println(zdtET);

        // Alternatively
        zdtET = ZonedDateTime.ofInstant(instant, ZoneId.of("America/New_York"));
        System.out.println(zdtET);
    }
}

Output:

2021-05-14T13:40:18.896Z
2021-05-14T09:40:18.896-04:00[America/New_York]
2021-05-14T09:40:18.896-04:00[America/New_York]

The Z in the output is the timezone designator for zero-timezone offset. It stands for Zulu and specifies the Etc/UTC timezone (which has the timezone offset of +00:00 hours).

Note: For any reason, if you need to convert this object of Instant to an object of java.util.Date, you can do so as follows:

Date date = Date.from(instant);

Learn more about the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

0

精彩评论

暂无评论...
验证码 换一张
取 消