0

How to get the current time of the device where the apk is installed? Here's my code, what should I change to get the current time depending on the region with Kotlin

val calendar = Calendar.getInstance()
            val date = SimpleDateFormat("yyyy-MM-dd hh:mm:ss")
            val t =   date.format(calendar.time)
            txt.text = t.toString()

Why I get the local time + 3h ??

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Lina
  • 553
  • 10
  • 34
  • nothing. What you get is the local time. And you don't need ``.toString`` the ``t`` since ``t`` is already a String. – Psytho Sep 01 '20 at 09:18
  • So, why I get the local time + 3h ?? – Lina Sep 01 '20 at 09:20
  • No idea, check if the time zone on your device is set correctly. – Psytho Sep 01 '20 at 09:21
  • If I get a time from server and I want to change it to Local zone How can I do that ? – Lina Sep 01 '20 at 09:43
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Sep 02 '20 at 04:36
  • Beware of the case of format pattern letters. Check the difference between `hh` and `HH`. – Ole V.V. Sep 02 '20 at 04:37
  • Are you saying that your code gives a time that is 3 hours ahead of what the device clock displays? That’s weird and must mean that the JVM’s time zone differs from the device time zone. – Ole V.V. Sep 02 '20 at 05:40

2 Answers2

0

You don't have to change anything, the code gives you the device's current time

However, since it may not be accurate for each device, it is recommended to receive it from the server in case of time.

0

Your code is implicitly using the JVM’s default time zone. This doesn’t seem to give you the result that you wanted. The solution is to specify time zone explicitly (which is usually a good idea anyway).

java.time

I am using java.time, the modern Java date and time API.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    
    ZoneId usersDesiredTimeZone = ZoneId.of("Africa/Tripoli");
    ZonedDateTime now = ZonedDateTime.now(usersDesiredTimeZone);
    
    String text = now.format(formatter);
    System.out.println(text);

When I ran this snippet just now, the output was:

2020-09-02 16:51:21

The time printed is 2 hours ahead of UTC because this is what Libya is using. Please substitute your own desired time zone.

Yet another change from your code is I am using uppercase HH for hour of day from 00 through 23 in the format pattern string. Lowercase hh is for hour within AM or PM from 01 through 12. I figured this was probably not what you had wanted.

Question: Doesn’t java.time require Android API level 26?

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

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161