javaandroiddatetimekotlinandroid-jodatime

How to convert GMT +09:00 to local time?


When I print date that I get from server, it shows Mon Jun 24 16:15:31 GMT+09:00 2019

val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val date: Date? = formatter.parse(checkedDate) // date from server
val transformedDate = ("${String.format("%02d", date!!.month + 1)}.${String.format("%02d", date!!.date)}.${date.year + 1900}")
val title: String? = ("$transformedDate")

val longGmtTime = date.time
val mZone = TimeZone.getDefault()
val offset = mZone.getOffset(longGmtTime)
val longLocalTime = longGmtTime + offset - (9 * HOUR)

val localDate = Date() // local date
localDate.time = longLocalTime
val localFormatTime = formatter.format(localDate)
val transformedLocalDate = ("${String.format("%02d", localDate!!.month + 1)}.${String.format("%02d", localDate!!.date)}.${localDate.year + 1900}")

And it gives me server time: 2019-06-24 16:15:31 -> 06.24.2019, local time(Asia/Seoul)-> 2019-06-25 01:15:30 ->06.25.2019 for the result.

The server time and local time must be the same. But the local time shows somewhere else.

What's the problem?


Solution

  • What's the problem?

    The gross problem list includes:

    The fix: java.time

        ZoneId serverTimeZone = ZoneId.of("Asia/Seoul");
        DateTimeFormatter serverFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        ZoneId clientTimeZone = ZoneId.systemDefault();
    
        String checkedDate = "2019-06-24 16:15:31";
        ZonedDateTime serverDateTime = LocalDateTime.parse(checkedDate, serverFormatter)
                .atZone(serverTimeZone);
        ZonedDateTime clientDateTime = serverDateTime.withZoneSameInstant(clientTimeZone);
        System.out.println("clientDateTime: " + clientDateTime);
    

    Sorry that I can write and run only Java code, I trust you to translate. With my JVM’s time zone still set to Asia/Seoul I get:

    clientDateTime: 2019-06-24T16:15:31+09:00[Asia/Seoul]

    The server time and the client time are the same, as you requested. If instead I keep my own time zone, I get:

    clientDateTime: 2019-06-24T09:15:31+02:00[Europe/Copenhagen]

    So there is a conversion taking place.

    To format the date:

        DateTimeFormatter displayFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
                .withLocale(Locale.forLanguageTag("ko-KR"));
        String transformedLocalDate = clientDateTime.format(displayFormatter);
        System.out.println("transformedLocalDate: " + transformedLocalDate);
    

    transformedLocalDate: 2019. 6. 24.

    Or if you insist on month.date.year:

        DateTimeFormatter displayFormatter = DateTimeFormatter.ofPattern("MM.dd.u");
    

    transformedLocalDate: 06.24.2019

    A further recommendation would be to have your server deliver a date-time string in UTC in ISO 8601 format. That would go like 2019-06-24T07:15:31Z for the moment used in the examples.

    Question: Can I use java.time with minSdk API level 23 on Android?

    Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

    Links