Skip to content

Instantly share code, notes, and snippets.

@nadar71
Created October 21, 2020 09:40
Show Gist options
  • Save nadar71/314637c1d35c7eafbbadc89db9ec7a6b to your computer and use it in GitHub Desktop.
Save nadar71/314637c1d35c7eafbbadc89db9ec7a6b to your computer and use it in GitHub Desktop.
Returns the number of milliseconds (UTC time) for today's date at midnight in the local time zone
// Returns the number of milliseconds (UTC time) for today's date at midnight in the local time zone
val normalizedUtcMsForToday: Long
get() {
val utcNowMillis = System.currentTimeMillis()
val currentTimeZone = TimeZone.getDefault()
val gmtOffsetMillis = currentTimeZone.getOffset(utcNowMillis).toLong()
val timeSinceEpochLocalTimeMillis = utcNowMillis + gmtOffsetMillis
val daysSinceEpochLocal =
TimeUnit.MILLISECONDS.toDays(timeSinceEpochLocalTimeMillis)
return TimeUnit.DAYS.toMillis(daysSinceEpochLocal)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment