Skip to content

Instantly share code, notes, and snippets.

@petitviolet
Created August 13, 2017 06:46
Show Gist options
  • Select an option

  • Save petitviolet/db171d9c8cf023c76a02cd66452ab2cd to your computer and use it in GitHub Desktop.

Select an option

Save petitviolet/db171d9c8cf023c76a02cd66452ab2cd to your computer and use it in GitHub Desktop.
JodaTime <-> LocalDateTime snippet
object DateTimeConverters {
implicit class DateTimeConverter(val ldt: LocalDateTime) extends AnyVal {
def asJoda: DateTime = {
val instant = ldt.atZone(ZoneId.systemDefault()).toInstant
new DateTime(instant.toEpochMilli)
}
}
implicit class JodaConverter(val jdt: DateTime) extends AnyVal {
def asLocalDateTime: LocalDateTime = {
LocalDateTime.of(
jdt.getYear,
jdt.getMonthOfYear,
jdt.getDayOfMonth,
jdt.getHourOfDay,
jdt.getMinuteOfHour,
jdt.getSecondOfMinute,
jdt.getMillisOfSecond * 1000000
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment