scala java.time.ZonedDateTime.now时区错误
我想使用以下代码从香港时区获取当前日期:scala java.time.ZonedDateTime.now时区错误,scala,databricks,Scala,Databricks,我想使用以下代码从香港时区获取当前日期: java.time.ZonedDateTime.now( ZoneId.of( "<timezonename>" ) ) 有人有这个想法吗 @ java.time.ZonedDateTime.now( java.time.ZoneId.of( "Africa/Tunis" ) ) res1: ZonedDateTime = 2020-06-30T18:50:23.117+01:00[Afri
java.time.ZonedDateTime.now( ZoneId.of( "<timezonename>" ) )
有人有这个想法吗
@ java.time.ZonedDateTime.now( java.time.ZoneId.of( "Africa/Tunis" ) )
res1: ZonedDateTime = 2020-06-30T18:50:23.117+01:00[Africa/Tunis]
工作。您必须导入@Luis Miguel Mejía Suárez注意到的
ZoneId
,或者使用全名。Luis Miguel Mejía Suárez已经提到了这个问题。
但你也可以用这种方式:
import java.text.SimpleDateFormat
import java.util.{Date, TimeZone}
val tz = TimeZone.getTimeZone("Asia/Dhaka")
val df = new SimpleDateFormat("HH:mm Z")
df.setTimeZone(tz)
println(df.format(new Date()))
您是否导入了
ZoneId
?我如何才能只获取日期YYYY-MM-DD java.time.ZonedDateTime.now(java.time.ZoneId.of(“香港”))。toString()输出字符串=2020-07-01T15:55:02.356+08:00[香港]
import java.text.SimpleDateFormat
import java.util.{Date, TimeZone}
val tz = TimeZone.getTimeZone("Asia/Dhaka")
val df = new SimpleDateFormat("HH:mm Z")
df.setTimeZone(tz)
println(df.format(new Date()))