【问题标题】:Can't change the TimeZone into GMT/UTC+7无法将时区更改为 GMT/UTC+7
【发布时间】:2019-01-21 10:13:07
【问题描述】:

我的服务器有数据返回

 "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。

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

【问题讨论】:

  • 顺便考虑扔掉早已过时且臭名昭著的麻烦SimpleDateFormat和朋友,并将ThreeTenABP添加到您的Android项目中,以便使用java.time,现代Java日期和时间API .使用起来感觉好多了。
  • 优于偏移量,如+07:00,使用实时时区,如亚洲/克拉斯诺亚尔斯克或印度/圣诞节。它更自然地传达了为什么。亚洲/雅加达可能适合您?

标签: android timezone


【解决方案1】:

试试这个

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();
                }

【讨论】:

  • 谢谢兄弟,它的工作, outputDate 应该在 try catch 块内
猜你喜欢
  • 2014-04-22
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
  • 2018-04-02
相关资源
最近更新 更多