Giter Site home page Giter Site logo

Question: how to properly convert between LocalDateTime (or LocalDate/LocalTime with what's available) and Date/Calendar? about threetenabp HOT 3 CLOSED

jakewharton avatar jakewharton commented on July 22, 2024
Question: how to properly convert between LocalDateTime (or LocalDate/LocalTime with what's available) and Date/Calendar?

from threetenabp.

Comments (3)

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024 2

ok, so I've made some nice helper functions, for all conversions:

Date<->LocalDate

fun Date.toLocalDate(): LocalDate = LocalDate.of(year + 1900, month + 1, date)
fun LocalDate.toDate(): Date = Date(year - 1900, monthValue - 1, dayOfMonth)

Calendar<->LocalDate

fun Calendar.resetTimeFields(): Calendar {
    set(Calendar.HOUR_OF_DAY, 0)
    set(Calendar.SECOND, 0)
    set(Calendar.MINUTE, 0)
    set(Calendar.MILLISECOND, 0)
    return this
}

fun Calendar.toLocalDate(): LocalDate = LocalDate.of(get(Calendar.YEAR), get(Calendar.MONTH) + 1, get(Calendar.DAY_OF_MONTH))

fun LocalDate.toCalendar(): Calendar {
    val cal = Calendar.getInstance().resetTimeFields()
    cal.timeZone
    cal.set(year, monthValue - 1, dayOfMonth)
    return cal
}

epochTimeInSeconds<->LocalDate

    @JvmStatic
    fun getFromEpochSecond(epochSecond: Long): LocalDate = Instant.ofEpochSecond(epochSecond).atZone(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0))).toLocalDate()

fun LocalDate.toEpochSecond(): Long = atStartOfDay(ZoneId.ofOffset("UTC", ZoneOffset.ofHours(0))).toEpochSecond()

Just thought it might be useful...

from threetenabp.

AndroidDeveloperLB avatar AndroidDeveloperLB commented on July 22, 2024

In Kotlin, I've made these extension functions for Calendar<->LocalDate :

fun Calendar.toLocalDate(): LocalDate = LocalDate.of(get(Calendar.YEAR), get(Calendar.MONTH) + 1, get(Calendar.DAY_OF_MONTH))

fun LocalDate.toCalendar(): Calendar {
    val cal = Calendar.getInstance().resetTimeFields()
    cal.set(year, monthValue - 1, dayOfMonth)
    return cal
}

Are those correct?

from threetenabp.

JakeWharton avatar JakeWharton commented on July 22, 2024

Usage questions for ThreeTenBP can be asked on StackOverflow. This library is just a repackaging of the timezone database.

from threetenabp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.