Skip to content

Instantly share code, notes, and snippets.

@RichardSilveira
Created July 16, 2017 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichardSilveira/a76a4666ec853f9476a7a913d52e7a3c to your computer and use it in GitHub Desktop.
Save RichardSilveira/a76a4666ec853f9476a7a913d52e7a3c to your computer and use it in GitHub Desktop.
import org.apache.log4j.Logger
import org.junit.Test
import java.time.*
import java.time.format.DateTimeFormatter
open class ZonedDateTimeWithEpochTest {
@Test
fun dateTimesExchanges_Between_Brazil_USA_West_Epoch_should_be_the_same() {
val dateInString = "16-07-2017 02:30:05 PM"
val currentDate = LocalDateTime.now(ZoneId.of("America/Boa_Vista"))
val ldtNow = LocalDateTime.of(LocalDate.now(), LocalTime.now())
val ldtSample = LocalDateTime.parse(dateInString, DateTimeFormatter.ofPattern(DATE_FORMAT))
val cuiabaZoneId = ZoneId.of("America/Boa_Vista")
val cuiabaDateTime = ldtSample.atZone(cuiabaZoneId)
val newYokZoneId = ZoneId.of("Asia/Singapore")
val nyDateTime: ZonedDateTime = cuiabaDateTime.withZoneSameInstant(newYokZoneId)
val cuiabaEpoch = cuiabaDateTime.toInstant().toEpochMilli()
val nyEpoch = nyDateTime.toInstant().toEpochMilli()
val otherEpoch = ldtSample.toInstant(ZonedDateTime.now().offset).toEpochMilli()
assert(cuiabaEpoch.equals(nyEpoch) && nyEpoch.equals(otherEpoch))
}
companion object {
private val LOG = Logger.getLogger(ZonedDateTimeWithEpochTest::class.java)
const val DATE_FORMAT: String = "dd-M-yyyy hh:mm:ss a"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment