kurt_steiner
11/23/2023, 12:51 PMval CURRENT_TIME_ZONE = TimeZone.currentSystemDefault()
fun now() = Clock.System.now().toLocalDateTime(CURRENT_TIME_ZONE).toInstant(TimeZone.UTC)
my timezone is asia/shanghai, and the test code is
fun `test now instant`() {
val nowInstant = now()
println(nowInstant)
println(CURRENT_TIME_ZONE)
}
the result is
2023-11-23T20:50:06.707128450Z
Asia/Shanghai
it is ok, but when using this now
in the application, everything goes wrong
how can I fix it ?Klitos Kyriacou
11/23/2023, 2:21 PM04:00:00.006 UTC
shows a time (presumably produced by now()
) of 04:00:00.003016035 UTC
. That looks right to me. The fact that your Windows task bar shows "8:45" means that you looked at the log 45 minutes later.kurt_steiner
11/23/2023, 2:48 PMKlitos Kyriacou
11/23/2023, 4:16 PMnow()
as Clock.System.now().toLocalDateTime(CURRENT_TIME_ZONE).toInstant(TimeZone.UTC)
, you seem to be using an Instant to present local time. If you want local time, use LocalTime
(drop the toInstant
from your function body).kurt_steiner
11/24/2023, 7:31 AMLocalTime
as now()
, can you give me an example ?kurt_steiner
11/24/2023, 7:33 AM@Schedule
, I have change the code into
@Scheduled(cron = "0 0 4 * * *", zone = "Asia/Shanghai")
fun refreshTasks() {
<http://logger.info|logger.info>("refresh tasks at ${now()}")
dailyAttendanceService.refreshDataDaily()
}
so I can't set the zone as system default timezone, is there any way to fix it ?Klitos Kyriacou
11/24/2023, 9:35 AMfun now(): LocalDateTime = Clock.System.now().toLocalDateTime(CURRENT_TIME_ZONE)
Then your log message will show 000400 as the local time.kurt_steiner
11/24/2023, 12:09 PM