Java 日期时间转换为给定时区

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

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

所以如果我总结一下,

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

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

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) );
chunbaise0601 回答:Java 日期时间转换为给定时区

在@ole v.v 的解释的帮助下,我将两个日期时间值分开1次2. 时区

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

//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 日期时间转换为给定时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持前端之家!

本文链接:https://www.f2er.com/3175501.html

大家都在问