Java将UTC时间戳转换为本地DateTime

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18691376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 10:13:53  来源:igfitidea点击:

Java convert UTC timestamp to local DateTime

javaandroiddatetimetimestamp

提问by kazemipouya

I know there are dozens of answered posts about converting UTC Time/Date To/From local time already but non helped me to figure out what my problem is. My question is: By having UTC timestamp, how can i get local DateTime? This is what I have right now but this just convert the timestamp to DateTime format.

我知道已经有很多关于将 UTC 时间/日期转换为本地时间的回答帖子,但没有帮助我弄清楚我的问题是什么。我的问题是:通过拥有 UTC 时间戳,我如何获得本地日期时间?这就是我现在所拥有的,但这只是将时间戳转换为 DateTime 格式。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getDefault());         
sdf.format(new Date(timestamp * 1000));

Edited: I'm saving the UTC timestamp on the cloud so every device (Android/iOS) can query and convert to it's time zone.

编辑:我将 UTC 时间戳保存在云上,以便每个设备(Android/iOS)都可以查询并转换为它的时区。

采纳答案by Biraj Zalavadia

Try this is working with me

试试这对我有用

public  String getDateCurrentTimeZone(long timestamp) {
        try{
            Calendar calendar = Calendar.getInstance();
            TimeZone tz = TimeZone.getDefault();
            calendar.setTimeInMillis(timestamp * 1000);
            calendar.add(Calendar.MILLISECOND, tz.getOffset(calendar.getTimeInMillis()));
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date currenTimeZone = (Date) calendar.getTime();
            return sdf.format(currenTimeZone);
        }catch (Exception e) {
        }
        return "";
    }

回答by Ruchira Gayan Ranaweera

You can try this

你可以试试这个

 String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss a z" ;
 final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
 sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
 String dateTimeString =  sdf.format(new Date());
 System.out.println(dateTimeString);   // current UTC time
 long timeStamp=sdf.parse(dateTimeString).getTime(); //current UTC time in milisec
 Calendar cal = Calendar.getInstance();
 cal.setTime(new Date(timeStamp));
 cal.add(Calendar.HOUR_OF_DAY, 5);
 cal.add(Calendar.MINUTE, 30);
 System.out.println(sdf.format(cal.getTime()));  // time relevant to UTC+5.30

回答by Anand Jagtap

U can use Joda time to convert local to UTC and vice-versa e.g Local to UTC

您可以使用 Joda 时间将本地转换为 UTC,反之亦然,例如本地转换为 UTC


DateTime dateTimeNew = new DateTime(date.getTime(), DateTimeZone.forID("Asia/Calcutta"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    String datetimeString = dateTimeNew.toString("yyyy-MM-dd HH:mm:ss");
    long milis = 0;
    try {
        milis = simpleDateFormat.parse(datetimeString).getTime();
    } catch (ParseException e) {
        e.printStackTrace();
    }