首页 > 解决方案 > 将时间转换为另一个时区

问题描述

我有一个简单的代码,我想将某个给定时间转换为另一个时区,在我的情况下,时间是英国本地时间,但如果用户居住在不同的国家,我需要将时间转换为另一个时区,我试过了这个简单的代码,但它对我不起作用,它给了我随机时间 04:00 任何帮助将不胜感激


        var localTime = "16:00" // simulating time to Uk timezone
        localtime.text = localTime

        timeZone.setOnClickListener {
            val localTimes = "16:00"
            val timeFormatter = SimpleDateFormat("hh:mm", Locale.UK)
            val timezone = TimeZone.getDefault() // get device timezone
            timeFormatter.timeZone = timezone
            val timeToFormat = timeFormatter.parse(localTimes)
            val formattedTime = timeFormatter.format(timeToFormat)
            localtime.text = formattedTime
        }

标签: androidkotlin

解决方案


推荐阅读