Joda API似乎返回了错误的结果。我有两个日期要比较。其中一种格式为YYYY-MM-DD HH:mm:ss,另一种格式为YYYYMMDD。阅读有关前者不是标准格式的帖子后,我将其转换为与后者(YYYYMMDD)相同的格式。但是,结果仍然是不可接受的。我尝试了DateTimeFormatterBuilder,并手动将日期包含在所需的格式中。以下是我的代码。我知道daysBetween正在制作一个月份的差异,可能我应该使用其他一些API来实际获得差异。但是,在这个例子中,底线显示为大于日期2,这是错误的。 仅供参考,我使用nscala-time作为scala代码的包装器。它在内部调用Joda API。我的代码有什么问题,或者它是库中的错误吗?
注意:当年份不同时,结果是正确的。因此,比较似乎只是比较年份部分。
以下是代码:
package util
import com.github.nscala_time.time.Imports._
import com.github.nscala_time.time.StaticDateTime
import com.github.nscala_time.time.StaticLocalDateTime
//import org.joda.time.*;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.DateTimeFormatterBuilder
import org.joda.time.DateTimeComparator;
import org.joda.time.Months
import org.joda.time.Days
object dateFormats {
val YYYYMMDD = DateTimeFormat.forPattern("YYYYMMDD")
//val YYYYMMDDHHMISS = DateTimeFormat.forPattern("YYYY-MM-DD HH:mm:ss")
val YYYYMMDDHHMISS = new DateTimeFormatterBuilder().appendYear(4, 4).appendLiteral('-').appendMonthOfYear(2).appendLiteral('-').appendDayOfMonth(2).appendLiteral(' ').appendHourOfDay(2).appendLiteral(':').appendMinuteOfHour(2).appendLiteral(':').appendSecondOfMinute(2).toFormatter()
}
object TestJoda {
def toDateTime(dtString: String, fmt: DateTimeFormatter): DateTime = {
//println("date is " + dtString + " Format is " + fmt)
fmt.parseDateTime(dtString)
}
def toEpoch(dtString: String, fmt: DateTimeFormatter): Long =
toDateTime(dtString, fmt).getMillis()
def monthsBetween(FromDT: String, fmt1: DateTimeFormatter, ToDT: String, fmt2: DateTimeFormatter): Int =
Months.monthsBetween(toDateTime(FromDT, fmt1), toDateTime(ToDT, fmt2)).getMonths
def daysBetween(FromDT: String, fmt1: DateTimeFormatter, ToDT: String, fmt2: DateTimeFormatter): Int =
Days.daysBetween(toDateTime(FromDT, fmt1), toDateTime(ToDT, fmt2)).getDays
def compareDT(FromDT: String, fmt1: DateTimeFormatter, ToDT: String, fmt2: DateTimeFormatter): Int =
DateTimeComparator.getInstance().compare(toDateTime(FromDT, fmt1), toDateTime(ToDT, fmt2))
def isGreaterDT(FromDT: String, fmt1: DateTimeFormatter, ToDT: String, fmt2: DateTimeFormatter): Boolean = {
compareDT(FromDT, fmt1, ToDT, fmt2) match {
case -1 | 0 => false
case 1 => true
}
}
def convertToYYYYMMDD( in : String): String = {
val out = in .substring(0, 4) + in .substring(5, 7) + in .substring(8, 10)
println("YYYY =" + in .substring(0, 4) + "MM =" + in .substring(5, 7) + "DD =" + in .substring(8, 10))
out
}
def testJoda() = {
// val d1 = "2005-04-17 00:00:00"
val d1 = convertToYYYYMMDD("2005-04-17 00:00:00")
val d2 = "20051107"
//val COMPUTE_DATE_DIFF = isGreaterDT(d1,(dateFormats.YYYYMMDDHHMISS),d2,(dateFormats.YYYYMMDD))
val COMPUTE_DATE_DIFF = isGreaterDT(d1, (dateFormats.YYYYMMDD), d2, (dateFormats.YYYYMMDD))
println("As per Joda " + d1 + " is greater than " + d2 + " is " + COMPUTE_DATE_DIFF)
val dbtn = daysBetween(d2, (dateFormats.YYYYMMDD), d1, (dateFormats.YYYYMMDD))
println("days between are" + dbtn)
//val d1e = toEpoch(d1,dateFormats.YYYYMMDDHHMISS)
val d1e = toEpoch(d1, dateFormats.YYYYMMDD)
println("d1 to epoch is" + d1e)
val d2e = toEpoch(d2, dateFormats.YYYYMMDD)
println("d2 to epoch is" + d2e)
}
def main(x: Array[String]): Unit = {
val returned = testJoda()
println(returned)
}
}
输出结果为:
YYYY =2005MM =04DD =17
As per Joda 20050417 is greater than 20051107 is true
days between are10
d1 to epoch is1105948800000
d2 to epoch is1105084800000
答案 0 :(得分:0)
代码中有错误。日期格式说明符需要' dd'而不是DD。这解决了错误