Time offset between two dates

If I understand correctly, startTs and localizedDateTime represent exactly the same event timestamp, but in different time zones - respectively UTC and some time zone, that’s local to the event, and your main interest is to retrieve the offset of the event-local time zone, is that right?
If so, it seems that you may be interested in using ZoneOffset and OffsetDateTime

See your example adapted to use them. It seems to follow the formatting you want:

import java.time.Duration
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.time.format.DateTimeFormatterBuilder
import java.time.temporal.ChronoField
import java.util.*

//sampleStart
fun parseDateTime(startTs: Long, endTs: Long, localizedDateTime: String): Pair<String, String> {
    val dateUtcStart = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTs), ZoneOffset.UTC)
    val dateUtcEnd = LocalDateTime.ofInstant(Instant.ofEpochMilli(endTs), ZoneOffset.UTC)

    val formatter = DateTimeFormatterBuilder()
        .parseCaseInsensitive()
        .parseDefaulting(ChronoField.YEAR_OF_ERA, dateUtcStart.year.toLong())
        .append(DateTimeFormatter.ofPattern("E dd MMM hh:mm a"))
        .toFormatter(Locale.ENGLISH)

    val localDateTime = LocalDateTime.parse(localizedDateTime, formatter)

    val duration = Duration.between(dateUtcStart, localDateTime)
    val offset = ZoneOffset.ofTotalSeconds(duration.seconds.toInt())
    val startWithOffset = dateUtcStart.atOffset(offset)
    val endWithOffset = dateUtcEnd.atOffset(offset)

    return Pair(startWithOffset.toString(), endWithOffset.toString())
}

fun main() {
    val pair1 = parseDateTime(1626998400000, 1627005600000, "Fri 23 Jul 10:30 am")
    val pair2 = parseDateTime(1626998400000, 1627005600000, "Thu 22 Jul 11:30 pm")

    println(pair1)
    println(pair2)
}
//sampleEnd
2 Likes