我是科特林新手。我遇到了一个问题。
我有这个代码:
val sdf = SimpleDateFormat("dd.MM.yyyy")
val currentDate = sdf.format(Date())
println(currentDate)
val stringDate = "12.03.2015"
val dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy", Locale.ENGLISH)
val millisecondsSinceEpoch = LocalDate.parse(stringDate, dateFormatter)
.atStartOfDay(ZoneOffset.UTC)
.toInstant()
.toEpochMilli()
println(millisecondsSinceEpoch)
val time = currentDate - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
Run Code Online (Sandbox Code Playgroud)
但上线了:
val time = currentDate - millisecondsSinceEpoch
val Datee = sdf.format(time)
println(Datee)
Run Code Online (Sandbox Code Playgroud)
我收到错误:
java.lang.IllegalArgumentException: Cannot format given Object as a Date
Run Code Online (Sandbox Code Playgroud)
请帮助我如何解决这个问题。我需要从字符串中的日期中减去当前日期。
更新:
如何正确地从一个日期减去另一个日期并得到天数差异?