我想将joda.time.DateTime
与ISODateTimeFormat
解析为以下格式yyyy-MM-dd HH:mm:ss
,其中HH:mm:ss
被截断为全零。我已经使用withTimeAtStartOfDay部分实现了目标。
下面的代码以2019-11-28T00:00:00.000+03:00
代替2019-11-28 00:00:00
val dateTime: DateTime = new DateTime().minusDays(1).withTimeAtStartOfDay
我是否纠正为无法通过ISODateTimeFormat
实现这一目标?我曾尝试使用DateTimeFormat
并奏效,但想了解是否有更好的方法。
val timeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")
println(dateTime.toString(timeFormatter)) // 2019-11-28 00:00:00
首先,您实际上应该不再使用JodaTime,而只需使用java.time
中的类(JodaTime已集成到Java 8中。)>
其次,您基本上只想处理日期。然后,您可以简单地使用LocalDate
并以您的格式输出固定的文本部分,因为您知道它始终为“ 00:00:00”:
import java.time._
import java.time.format._
val yesterday = LocalDate.now.minusDays(1)
println(DateTimeFormatter.ISO_LOCAL_DATE.format(yesterday) + " 00:00:00")