如何使用 kotlin 获取格林威治标准时间的星期几?
How to get day of the week of GMT using kotlin?
例如,当前 UTC 时间为:
17:14:24 协调世界时
2021 年 11 月 5 日星期五
我想要得到结果“6”(星期日 = 1 => 星期五 = 6)
使用kotlinx.datetime
(多平台):
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.toLocalDateTime
public val DayOfWeek.dayNumberStartingFromSunday: Int
get() = when (this) {
DayOfWeek.SUNDAY -> 1
else -> isoDayNumber + 1
}
fun main() {
// val now: Instant = Clock.System.now()
val now = Instant.parse("2021-11-05T17:14:24Z")
val datetimeInUtc = now.toLocalDateTime(TimeZone.UTC)
val dayNumberStartingFromSunday = datetimeInUtc.dayOfWeek.dayNumberStartingFromSunday
println(dayNumberStartingFromSunday) // 6
}
一周的第一天是 Locale
特定的。由于您希望一周的第一天是 Sunday
,您可以使用 Locale.US
.
演示:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getDayOfWeekValue(LocalDateTime.of(2021, 11, 5, 17, 14, 24)));
}
static int getDayOfWeekValue(LocalDateTime input) {
return Math.toIntExact(
ChronoUnit.DAYS.between(
input.with(
TemporalAdjusters.previousOrSame(
WeekFields.of(Locale.US)
.getFirstDayOfWeek())),
input.plusDays(1)));
// Note: One day has been added as ChronoUnit.DAYS.between excludes
// the second parameter while calculating the number of days
}
}
输出:
6
注意:使用Locale.UK
(一周的第一天是星期一)测试此代码,您将得到 5 作为输出。根据您的要求,您可以更改函数的定义,例如
static int getDayOfWeekValue(LocalDateTime input, Locale locale) {
return Math.toIntExact(
ChronoUnit.DAYS.between(
input.with(
TemporalAdjusters.previousOrSame(
WeekFields.of(locale)
.getFirstDayOfWeek())),
input.plusDays(1)));
// Note: One day has been added as ChronoUnit.DAYS.between excludes
// the second parameter while calculating the number of days
}
详细了解 modern Date-Time API* from Trail: Date Time。
* 如果您正在为 Android 项目工作,并且您的 Android API 水平仍然不符合 Java-8,请检查Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time
. Check this answer and this answer 学习如何使用 java.time
API 和 JDBC。
例如,当前 UTC 时间为:
17:14:24 协调世界时 2021 年 11 月 5 日星期五
我想要得到结果“6”(星期日 = 1 => 星期五 = 6)
使用kotlinx.datetime
(多平台):
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.Instant
import kotlinx.datetime.TimeZone
import kotlinx.datetime.isoDayNumber
import kotlinx.datetime.toLocalDateTime
public val DayOfWeek.dayNumberStartingFromSunday: Int
get() = when (this) {
DayOfWeek.SUNDAY -> 1
else -> isoDayNumber + 1
}
fun main() {
// val now: Instant = Clock.System.now()
val now = Instant.parse("2021-11-05T17:14:24Z")
val datetimeInUtc = now.toLocalDateTime(TimeZone.UTC)
val dayNumberStartingFromSunday = datetimeInUtc.dayOfWeek.dayNumberStartingFromSunday
println(dayNumberStartingFromSunday) // 6
}
一周的第一天是 Locale
特定的。由于您希望一周的第一天是 Sunday
,您可以使用 Locale.US
.
演示:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getDayOfWeekValue(LocalDateTime.of(2021, 11, 5, 17, 14, 24)));
}
static int getDayOfWeekValue(LocalDateTime input) {
return Math.toIntExact(
ChronoUnit.DAYS.between(
input.with(
TemporalAdjusters.previousOrSame(
WeekFields.of(Locale.US)
.getFirstDayOfWeek())),
input.plusDays(1)));
// Note: One day has been added as ChronoUnit.DAYS.between excludes
// the second parameter while calculating the number of days
}
}
输出:
6
注意:使用Locale.UK
(一周的第一天是星期一)测试此代码,您将得到 5 作为输出。根据您的要求,您可以更改函数的定义,例如
static int getDayOfWeekValue(LocalDateTime input, Locale locale) {
return Math.toIntExact(
ChronoUnit.DAYS.between(
input.with(
TemporalAdjusters.previousOrSame(
WeekFields.of(locale)
.getFirstDayOfWeek())),
input.plusDays(1)));
// Note: One day has been added as ChronoUnit.DAYS.between excludes
// the second parameter while calculating the number of days
}
详细了解 modern Date-Time API* from Trail: Date Time。
* 如果您正在为 Android 项目工作,并且您的 Android API 水平仍然不符合 Java-8,请检查Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time
. Check this answer and this answer 学习如何使用 java.time
API 和 JDBC。