JAVA UTC to EST from a long UTC timestamp

三世轮回 提交于 2019-12-08 00:39:04

问题


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


回答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)




回答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.




回答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.



来源:https://stackoverflow.com/questions/2624610/java-utc-to-est-from-a-long-utc-timestamp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!