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

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

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

所以如果我总结一下,

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

如何在 Java 中实现这一点?我已经尝试了下面的代码,但它比当前时间少 08 小时,即

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 的解释的帮助下,我将两个日期时间值分开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;
    }

相关文章