0

So my app fetches from information from a JSON file from a server, of which there are a few date/time values. The time and date are in UTC. I need to display these in my app, in the users local timezone.

Example of the data from JSON:

"start":"2018-10-20 03:00:00","finish":"2018-10-20 05:00:00"

My code so far, which display the date and time fine, in UTC..

val dateStringStart = radioScheduleDMList.get(position).start
        val dateStringEnd = radioScheduleDMList.get(position).finish

        val date = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).parse(dateStringStart)
        val dateEnd = SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault()).parse(dateStringEnd)
        val day = SimpleDateFormat("d MMM").format(date)
        val startDate = SimpleDateFormat("ha").format(date)
        val endDate = SimpleDateFormat("ha").format(dateEnd)

How can I go about displaying this data using the devices timezone? I've been googling for hours.

Using the above example, my app shows "20 OCT" for the date, and "3AM-5AM" for the time. In my case, I live in Australia (GMT+10) so I would expect day "20 OCT" and "1PM-3PM". In short, I want to detect the user’s timezone offset from UTC and apply it for display.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
wishie
  • 45
  • 6
  • 1
    can you share expected out of this start value ? – Syed Hamza Hassan Oct 20 '18 at 05:27
  • Using the above example, my app would show "20 OCT" for the date, and "3AM-5AM" for the time. In my case, I live in Australia (GMT+10) so I would expect it to day "20 OCT" and "1PM-3PM" – wishie Oct 20 '18 at 08:10
  • In short, I want to detect the users timezone offset from UTC and apply it for display. – wishie Oct 20 '18 at 08:24
  • You may consider not using `SimpleDateFormat` and `Date`. Those were the classes we used in the old days, but they were poorly designed and are now considered long outdated. The replacement is in [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). To use it on not brand new Android, go through [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP), the backport adapted for Android. – Ole V.V. Oct 21 '18 at 18:08
  • 1
    Since you’ve been googling for hours, you should tell us what you’ve found and in what way it was insufficient or failed to solve your problem. Otherwise we’ll just repeat what was in those links that didn’t help you, which will be a sad waste of our time and will get you no further. – Ole V.V. Oct 22 '18 at 13:42

1 Answers1

0

java.time and ThreeTenABP

    DateTimeFormatter jsonFormatter = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("d MMM", Locale.forLanguageTag("en-AU"));
    DateTimeFormatter hourFormatter
            = DateTimeFormatter.ofPattern("ha", Locale.forLanguageTag("en-AU"));
    ZoneId zone = ZoneId.systemDefault();

    String dateStringStart = "2018-10-20 03:00:00";

    OffsetDateTime startDateTime = LocalDateTime.parse(dateStringStart, jsonFormatter)
            .atOffset(ZoneOffset.UTC);
    ZonedDateTime userStartDateTime = startDateTime.atZoneSameInstant(zone);
    String startDayString = userStartDateTime.format(dateFormatter);
    String startTimeString = userStartDateTime.format(hourFormatter);
    System.out.println("Start day:  " + startDayString);
    System.out.println("Start hour: " + startTimeString);

I’m sorry I don’t write Kotlin. Can you translate from Java on your own? When I run the above code on a JVM with default time zone Australia/Brisbane it outputs:

Start day:  20 Oct
Start hour: 1PM

Just do similarly for the end date and time.

Question: Can I use java.time on Android?

Yes, java.time works nicely on 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 Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

Links

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