Get current time in a given timezone : android

后端 未结 10 1976
悲哀的现实
悲哀的现实 2020-11-30 10:13

I am new to Android and I am currently facing an issue to get current time given the timezone.

I get timezone in the format \"GMT-7\" i.e. string. and I have the sys

10条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 10:31

    One way to deal with time zone and milliseconds values:

    val currentDateTime = Calendar.getInstance().apply {
            timeZone?.let {
                val tzCalendar = Calendar.getInstance(it)
                this.set(Calendar.HOUR_OF_DAY, tzCalendar.get(Calendar.HOUR_OF_DAY))
                this.set(Calendar.MINUTE, tzCalendar.get(Calendar.MINUTE))
            }
        }.time
    

    This way you're getting time in milliseconds for the specific timezone.

提交回复
热议问题