kotlin 无法将时间解析为正确的GMT

clj7thdc  于 2022-11-25  发布在  Kotlin
关注(0)|答案(1)|浏览(166)

从API我得到的时间字符串“13:00:00Z”在这种格式,它是GMT 0时区。
我想把它调整到我的时区,即GMT +4。

qv7cva1a

qv7cva1a1#

首先你把时间转换成UTC,然后把UTC时间转换成你的当地时区。例如:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
                    simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
                    try {
                        Date utcDate = simpleDateFormat.parse("Your date");
                        SimpleDateFormat yourDateFormat = new SimpleDateFormat("dd MMM yyyy · HH'h'mm", Locale.getDefault());
                        String dateString = simpleDateFormat.format(utcDate);
                        binding.orderdate.setText(dateString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

相关问题