3

I am trying to get the current date and time in ISO 8601 format. My code below returns the date 14 days later and incorrect time. I need the correct format and current date for an api request.

val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").apply {
        this.timeZone = TimeZone.getTimeZone("CST")
    }

val now = Calendar.getInstance(TimeZone.getTimeZone("CST"))

val sendDateUAT = formatter.format(now.time)
Log.d(TAG, "sendDate: $sendDateUAT")

This returns: 2019-08-05T02:53:40Z

EDIT: This is now.time:

java.util.GregorianCalendar[time=1564973620006,areFieldsSet=true,lenient=true,zone=GMT,firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=7,WEEK_OF_YEAR=32,WEEK_OF_MONTH=2,DAY_OF_MONTH=5,DAY_OF_YEAR=217,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=2,HOUR_OF_DAY=2,MINUTE=53,SECOND=40,MILLISECOND=6,ZONE_OFFSET=0,DST_OFFSET=0]
Quick learner
  • 10,632
  • 4
  • 45
  • 55
QThompson
  • 1,599
  • 3
  • 16
  • 40
  • 1
    Cannot reproduce this locally. Where are you executing this code? I suspect the clock is incorrect. – Phil Jul 22 '19 at 02:15
  • Can you verify the result of `now.time` is expected? i.e. just format it with standard UTC formatting to see what the result is and compare to your machines current time. Your format looks correct for extracting 6801; at least from several examples I've seen online. – Horatius Cocles Jul 22 '19 at 02:17
  • @HoratiusCocles I just updated my question with `now.time` result. – QThompson Jul 22 '19 at 02:38
  • @Phil I am executing this code within android studio. It should be getting the computer system time, which is set to CST. – QThompson Jul 22 '19 at 02:40
  • And just to clarify, your machine clock does not match the return of that method correct? – Horatius Cocles Jul 22 '19 at 02:41
  • @HoratiusCocles Yes I just checked my emulator and it is set to current date and correct timezone. – QThompson Jul 22 '19 at 02:46
  • As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [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. Jul 22 '19 at 04:27
  • 2
    1564973620006 is GMT: Monday 5. August 2019 02:53:40.006. Looks to me like the time of day is right for when you asked your question, but the date is 2 weeks ahead. Also `MONTH=7` means August (argh!) and you’ve got `DAY_OF_MONTH=5`. – Ole V.V. Jul 22 '19 at 04:31
  • I found the solution not sure how its much different from the code above but it works lol. I appreciate all of the input. – QThompson Jul 22 '19 at 12:56

2 Answers2

1

Here is a helper class that you can use to convert your dates:

public object DateHelper {
    private const val ISO_8601_24H_FULL_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"

    @JvmName("toIsoStringNullable")
    fun Date?.toIsoString(): String? {
        return this?.toIsoString()
    }

    fun Date.toIsoString(): String {
        val dateFormat: DateFormat = SimpleDateFormat(ISO_8601_24H_FULL_FORMAT)
        return dateFormat.format(this)
    }
}

It can be used very concisely like this:

Date().toIsoString()
Huseyin Yagli
  • 9,896
  • 6
  • 41
  • 50
-1

For some reason this fixed the problem and returned the correct date.

val today = Calendar.getInstance()
val sendDateUAT = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(today.time)
QThompson
  • 1,599
  • 3
  • 16
  • 40
  • 4
    Thaks for answering your own question. The answer is not correct, though. `Z` is an offset (of 0 from UTC), so appending it to a date and time that may have (usually has) a different offset gives an incorrect result. – Ole V.V. Jul 23 '19 at 05:36