无法将TimeZone更改为GMT / UTC + 7

时间:2019-01-21 10:13:08

标签: android timezone

我的服务器中有返回的数据

 "createdAt": "2019-01-21T09:55:01.546Z"

我已经将该字符串转换为Date 所以现在我的日期显示如下:

"Sunday, 01-12-2019 09.55 am"

我的问题是我希望时间偏移+7或UTC+7

我已经做到了:

String dateData = response.body().getCreatedAt();

            SimpleDateFormat inputDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            SimpleDateFormat outputDate = new SimpleDateFormat("EEEE, dd-MM-yyyy hh:mm a", Locale.getDefault());

            Date date = null;
            try{
                 date = inputDate.parse(dateData);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            inputDate.setTimeZone(TimeZone.getTimeZone("UTC"));
            outputDate.setTimeZone(TimeZone.getTimeZone("UTC+7"));

            mPosted.setText(outputDate.format(date));

,但结果仍然相同。而奇怪的是,当我更改outputDate.setTimeZone(TimeZone.getTimeZone("UTC"));时,时间结果更改为02.55 am。

如何将其更改为+7?所以 时间应该是4.55 pm

1 个答案:

答案 0 :(得分:0)

  

尝试一下

try {
                    SimpleDateFormat ip_format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
                    ip_format.setTimeZone(TimeZone.getTimeZone("UTC"));
                    SimpleDateFormat outFormat = new SimpleDateFormat("EEEE, dd-MM-yyyy hh:mm a", Locale.ENGLISH);
                    outFormat.setTimeZone(TimeZone.getTimeZone("GMT+07:00"));

                    Date date = null;
                    date = ip_format.parse(dateData);
                    System.out.println("date" + date.toString());
                } catch (Exception e) {
                    e.printStackTrace();
                }