https://kotlinlang.org logo
#ktor
Title
# ktor
h

Hien Nguyen

09/21/2021, 7:58 AM
Copy code
io.ktor.util.date.GMTDate
how to get timestamp of next month with GMTDate 😞 ?
a

Aleksei Tirman [JB]

09/21/2021, 9:11 AM
Do you mean the timestamp of a first day of the next month or the timestamp of a current date in a month?
h

Hien Nguyen

09/21/2021, 9:14 AM
like the add method of java.util.Calendar
Copy code
Consider a GregorianCalendar originally set to August 31, 1999. Calling add(Calendar.MONTH, 13) sets the calendar to September 30, 2000.
😔
a

Aleksei Tirman [JB]

09/21/2021, 10:05 AM
You can use the kotlinx-datetime library to achieve the necessary effect:
Copy code
val date = GMTDate(0, 0, 0, 31, io.ktor.util.date.Month.AUGUST, 1999)
val instant = Instant.fromEpochMilliseconds(date.timestamp)
val result = instant.plus(13, DateTimeUnit.MONTH, TimeZone.of("GMT"))
val local = result.toLocalDateTime(TimeZone.of("GMT"))
val newDate = GMTDate(seconds = local.second, minutes = local.minute, hours = local.hour, dayOfMonth = local.dayOfMonth, month = io.ktor.util.date.Month.from(local.monthNumber - 1), year = local.year)
println(newDate.toJvmDate())
Please file a feature request if you want to see this functionality in Ktor.
🙌 1
h

Hien Nguyen

09/21/2021, 10:11 AM
ok thx a lot, 😘 u save my day
h

hfhbd

09/21/2021, 10:30 AM
h

Hien Nguyen

09/21/2021, 10:31 AM
thx Aleksei and Phillip ^_^