1

I have a simple function to extract time from timestamp which is String.

timestamp(input)  : "2020-08-13T09:33:17Z"
outputTime(output): "09:33"
desired output    : "12:33"

function:

fun getTimeFromTimestamp(timestamp: String): String {
    val inputFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss\'Z\'", Locale.getDefault())
    val time = inputFormat.parse(timestamp) ?: Date()
    val outputFormat: DateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())
    val outputTime = outputFormat.format(time)

    return outputTime
}

Problem is that it does not take in effect of timezone in the output, which is GMT+3 in this case.

You can see my debug screenshot

How can I make sure that time is displayed including timezone that comes with it (GMT+3)?

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
MaaAn13
  • 264
  • 5
  • 24
  • 54
  • 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. Aug 13 '20 at 21:15
  • Does this answer your question? [parsing date/time to localtimezone](https://stackoverflow.com/questions/51206465/parsing-date-time-to-localtimezone) – Ole V.V. Aug 13 '20 at 21:17
  • You may also want to look at [Android Studio Convert ISO string to “America/New_York” when adding to event to calendar](https://stackoverflow.com/questions/52670961/android-studio-convert-iso-string-to-america-new-york-when-adding-to-event-to) – Ole V.V. Aug 13 '20 at 21:31

3 Answers3

2

The outputtime is in that format because you specified it as so ( HH:mm ) in this line

val outputFormat: DateFormat = SimpleDateFormat("HH:mm", Locale.getDefault())

if you want it to include the time zone append the time zone to the output string

edit

"HH:mm zZ" seems to be doing the trick for me check out this fiddle

m'hd semps
  • 564
  • 4
  • 14
  • To add to this: "Z" is for the timezone! So for example "HH:mm \'Z\'" would give you 09:54 GMT+3. Other possible values that can be used in the formatter can be found here https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Robbe Roels Aug 13 '20 at 13:13
  • @RobbeRoels Changing "HH:mm" to "HH:mm Z" returns 09:52 +0300 not 12:52. – MaaAn13 Aug 13 '20 at 13:42
  • @Ole V.V. edit clears up the air I would recommend his answer – m'hd semps Aug 14 '20 at 06:05
1

The problem is you import your start time "2020-08-13T09:33:17Z" AS GMT+3 and then try to format it to GMT+3. Try and format your import date to GMT+0 and then keep the format for your output to GMT+3

You can see this answer for more on how to set the timezone on your SimpleDateFormat: https://stackoverflow.com/a/18124407/4383500

This should convert your time from a GMT timestamp to the local time

  fun getTimeFromTimestamp(timestamp: String): String {
        val inputFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd\'T\'HH:mm:ss\'Z\'")
        inputFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        val time = inputFormat.parse(timestamp) ?: Date()
        val outputFormat: DateFormat = SimpleDateFormat("HH:mm ,z")
        inputFormat.setTimeZone(TimeZone.getDefault());
        val outputTime = outputFormat.format(time)
    return outputTime
Robbe Roels
  • 195
  • 4
  • 11
1

Use LocalDateTime [java.time] instead as SimpleDateFormat is not recommended in 2020.

Check this link

MdBasha
  • 423
  • 4
  • 16
  • 1
    I agree in java.time, not in `LocalDateTime`. I suggest `Instant.parse("2020-08-13T09:33:17Z").atZone(ZoneId.of("Europe/Tallinn")).toLocalTime()`. It yields `12:33:17`. Or one may use a `DateTimeFormatter` for formatting the `ZonedDateTime` from `atZone()` directly into the desired format, for excample, `12:33`. One should substitute the desired time zone, of course (and not use a naked offset like `+03:00`). – Ole V.V. Aug 13 '20 at 21:27