如何找到两个日期之间的天数?

时间:2019-01-17 10:08:55

标签: java scala

我想知道两个日期之间的天数。当我使用以下代码时,它将引发错误。我该如何解决?

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div data-value="1000">
  <a onclick="View();">View</a>
  <a onclick="Hide();">Hide</a>
</div>

错误:

DateTimeFormatter format = DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.ENGLISH);

LocalDateTime dExpCompletionDate = LocalDateTime.parse(sExpCompletionDate, format);
LocalDateTime dExpCommencementDate = LocalDateTime.parse(sExpCommencementDate, format);

3 个答案:

答案 0 :(得分:2)

joda.time已过时且过时。使用java.time

import java.time.LocalDate
import java.time.temporal.ChronoUnit.DAYS

DAYS.between(LocalDate.parse("2018-10-01")
            ,LocalDate.parse("2018-10-12"))  //res0: Long = 11

答案 1 :(得分:0)

您可以尝试这样的事情

import java.time.LocalDate 
import java.time.format.DateTimeFormatter


val start = "2018-10-01"
val end= "2018-10-12"


val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
val oldDate = LocalDate.parse(start, formatter)

val newDate = LocalDate.parse(end, formatter)
println(newDate.toEpochDay() - oldDate.toEpochDay())

答案 2 :(得分:0)

最后我找到了解决方法:

val format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")

val from = format.parseDateTime("2018-10-01 00:00:00")
val to = format.parseDateTime("2018-10-12 00:00:00")

println("Number of days between 2 period: " + Days.daysBetween(from, to).getDays)