For what I can only assume is a timezone issue, the following code yields different week of year values for the same day, but different times:
Note, the code is written in Kotlin
Example
fun main(args: Array<String>) {
val middaySundayAfterEpoch = Instant.EPOCH + Duration
.ZERO
.plusDays(3)
.plusHours(12)
val almostMidnightSundayAfterEpoch = Instant.EPOCH + Duration
.ZERO
.plusDays(3)
.plusHours(23)
println(getWeekOfYear(middaySundayAfterEpoch))
println(getWeekOfYear(almostMidnightSundayAfterEpoch))
}
fun getWeekOfYear(instant: Instant): Int {
val calendar: Calendar = Calendar.getInstance()
calendar.time = Date.from(instant)
return calendar.get(Calendar.WEEK_OF_YEAR)
}
Results
1
2
Assumptions
- Sunday, 12:00 is actually Sunday, 13:00 in the calendar
- Sunday, 23:00 is actually Monday, 00:00 in the calendar
Question
How do I modify this to ignore the time zone, so that both times occur in the same week?