Java日期时间转换为给定的时区 [英] Java Date Time conversion to given timezone

查看:63
本文介绍了Java日期时间转换为给定的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式为 Tue,30 Apr 2019 16:00:00 +0800 的DateTime,它是 RFC 2822格式化的日期

I have a DateTime in the format of Tue, 30 Apr 2019 16:00:00 +0800 which is RFC 2822 formatted date

我需要将其转换为+0800的DateTime中的给定时区

I need to convert this to the given timezone in the DateTime which is +0800

所以,如果我总结一下,

So if i summarized,

DateGiven = Tue, 30 Apr 2019 16:00:00 +0800
DateWanted = 01-05-2019 00:00:00

如何用Java实现这一目标?我尝试了以下代码,但比当前时间

How can i achieve this in Java? I have tried the below code but it gives 08 hours lesser than the current time which is

30-04-2019 08:00:00

我尝试过的代码

String pattern = "EEE, dd MMM yyyy HH:mm:ss Z";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date startDate = format.parse(programmeDetails.get("startdate").toString());

//Local time zone   
 SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

//Time in GMT
Date dttt= dateFormatLocal.parse( dateFormatGmt.format(startDate) );

推荐答案

在@ole v.v的帮助下,我将datetime值分隔为两个1次2.时区

with the help of @ole v.v's explanation i have separated the datetime value for two 1. time 2. timezone

然后我使用此编码提取与给定时区相关的日期时间

then i used this coding to extract the datetime which is related to the given timezone

//convert datetime to give timezone 
    private static String DateTimeConverter (String timeVal, String timeZone)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    SimpleDateFormat offsetDateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

        offsetDateFormat2.setTimeZone(TimeZone.getTimeZone(timeZone));
        String result =null;
        try {
            result = offsetDateFormat2.format(format.parse(timeVal));
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }

这篇关于Java日期时间转换为给定的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆