在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Kotlin/kotlinx-datetime开源软件地址(OpenSource Url):https://github.com/Kotlin/kotlinx-datetime开源编程语言(OpenSource Language):Kotlin 88.4%开源软件介绍(OpenSource Introduction):kotlinx-datetimeA multiplatform Kotlin library for working with date and time. See Using in your projects for the instructions how to setup a dependency in your project. Design overviewThere are a few guiding principles in the design of The library puts a clear boundary between physical time of an instant and a local, time-zone dependent civil time, consisting of components such as year, month, etc that people use when talking about time. We intentionally avoid entities in the library that mix both together and could be misused. However, there are convenience operations that take, for example, a physical instant and perform a calendar-based adjustment (such as adding a month); all such operation explicitly take a time-zone information as parameter to clearly state that their result depends on the civil time-zone rules which are subject to change at any time. The library is based on the ISO 8601 international standard, other ways to represent dates and times are out of its scope. Internationalization (such as locale-specific month and day names) is out the scope, too. TypesThe library provides the basic set of types for working with date and time:
Type use-casesHere is some basic advice on how to choose which of the date-carrying types to use in what cases:
OperationsWith the above types you can get the following operations done. Getting the current moment of timeThe current moment of time can be captured with the val clock: Clock = ...
val currentMoment = clock.now() An instance of val currentMoment = Clock.System.now() Converting an instant to local date and time components
The val currentMoment: Instant = Clock.System.now()
val datetimeInUtc: LocalDateTime = currentMoment.toLocalDateTime(TimeZone.UTC)
val datetimeInSystemZone: LocalDateTime = currentMoment.toLocalDateTime(TimeZone.currentSystemDefault())
Additional time zones can be acquired by their string identifier with the val tzBerlin = TimeZone.of("Europe/Berlin")
val datetimeInBerlin = currentMoment.toLocalDateTime(tzBerlin)
val kotlinReleaseDateTime = LocalDateTime(2016, 2, 15, 16, 57, 0, 0) An instant can be obtained from val kotlinReleaseInstant = kotlinReleaseDateTime.toInstant(TimeZone.of("UTC+3")) Getting local date components
val now: Instant = Clock.System.now()
val today: LocalDate = now.toLocalDateTime(TimeZone.currentSystemDefault()).date
// or more short
val today: LocalDate = Clock.System.todayIn(TimeZone.currentSystemDefault()) Note, that today's date really depends on the time zone in which you're observing the current moment.
val knownDate = LocalDate(2020, 2, 21) Getting local time components
val now: Instant = Clock.System.now()
val thisTime: LocalTime = now.toLocalDateTime(TimeZone.currentSystemDefault()).time
val knownTime = LocalTime(hour = 23, minute = 59, second = 12)
val timeWithNanos = LocalTime(hour = 23, minute = 59, second = 12, nanosecond = 999)
val hourMinute = LocalTime(hour = 12, minute = 13) Converting instant to and from unix timeAn Converting instant and local date/time to and from stringCurrently, val instantNow = Clock.System.now()
instantNow.toString() // returns something like 2015-12-31T12:30:00Z
val instantBefore = Instant.parse("2010-06-01T22:19:44.475Z") Alternatively,
"2010-06-01T22:19:44.475Z".toInstant()
"2010-06-01T22:19:44".toLocalDateTime()
"2010-06-01".toLocalDate()
"12:01:03".toLocalTime()
"12:0:03.999".toLocalTime() Instant arithmeticval now = Clock.System.now()
val instantInThePast: Instant = Instant.parse("2020-01-01T00:00:00Z")
val durationSinceThen: Duration = now - instantInThePast
val equidistantInstantInTheFuture: Instant = now + durationSinceThen
To get the calendar difference between two instants you can use val period: DateTimePeriod = instantInThePast.periodUntil(Clock.System.now(), TimeZone.UTC)
The difference can be calculated as an integer amount of specified date or time units: val diffInMonths = instantInThePast.until(Clock.System.now(), DateTimeUnit.MONTH, TimeZone.UTC) There are also shortcuts A particular amount of date/time units or a date/time period can be added to an val now = Clock.System.now()
val systemTZ = TimeZone.currentSystemDefault()
val tomorrow = now.plus(2, DateTimeUnit.DAY, systemTZ)
val threeYearsAndAMonthLater = now.plus(DateTimePeriod(years = 3, months = 1), systemTZ) Note that Date arithmeticThe similar operations with date units are provided for
Notice that instead of general Date + time arithmeticArithmetic on Therefore, the recommended way to use a val timeZone = TimeZone.of("Europe/Berlin")
val localDateTime = LocalDateTime.parse("2021-03-27T02:16:20")
val instant = localDateTime.toInstant(timeZone)
val instantOneDayLater = instant.plus(1, DateTimeUnit.DAY, timeZone)
val localDateTimeOneDayLater = instantOneDayLater.toLocalDateTime(timeZone)
// 2021-03-28T03:16:20, as 02:16:20 that day is in a time gap
val instantTwoDaysLater = instant.plus(2, DateTimeUnit.DAY, timeZone)
val localDateTimeTwoDaysLater = instantTwoDaysLater.toLocalDateTime(timeZone)
// 2021-03-29T02:16:20 ImplementationThe implementation of date/time types, such as
Known/open issues, work TBD
Using in your projects
The library is published to Maven Central. The library is compatible with the Kotlin Standard Library not lower than If you target Android devices running below API 26, you need to use Android Gradle plugin 4.0 or newer and enable core library desugaring. Gradle
repositories {
mavenCentral()
}
kotlin {
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
}
}
}
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0")
} Note about time zones in JSBy default, there's only one time zone available in Kotlin/JS: the If you want to use all time zones in Kotlin/JS platform, you need to add the following npm dependency: kotlin {
sourceSets {
val jsMain by getting {
dependencies {
implementation(npm("@js-joda/timezone", "2.3.0"))
}
}
}
} and after that add the following initialization code in your project: @JsModule("@js-joda/timezone")
@JsNonModule
external object JsJodaTimeZoneModule
private val jsJodaTz = JsJodaTimeZoneModule MavenAdd a dependency to the <dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-datetime-jvm</artifactId>
<version>0.4.0</version>
</dependency> BuildingBefore building, ensure that you have thirdparty/date submodule initialized and updated. IDEA does that automatically when cloning the repository, and if you cloned it in the command line, you may need to run additionally: git submodule init
git submodule update The project requires JDK 8 to build classes and to run tests.
Gradle will try to find it among the installed JDKs or provision it automatically if it couldn't be found.
The path to JDK 8 can be additionally specified with the environment variable After that, the project can be opened in IDEA and built with Gradle. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论