【问题标题】:Joda DateTime library shows incorrect resultsJoda DateTime 库显示不正确的结果
【发布时间】:2015-02-02 03:07:31
【问题描述】:

Joda API 似乎返回了不正确的结果。我有 2 个日期要比较。其中一个是 YYYY-MM-DD HH:mm:ss 格式,另一个是 YYYYMMDD 格式。在阅读了有关前者不是标准格式的帖子后,我将其转换为与后者相同的格式(YYYYMMDD)。然而,结果仍然是不可接受的。我尝试了 DateTimeFormatterBuilder 并手动将日期子串化为所需的格式。下面是我的代码。我知道 daysBetween 正在制作一个月中的几天的差异,可能我应该使用其他一些 API 来实际获得差异。但是,底线是,在此示例中,日期 1 显示为大于日期 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

【问题讨论】:

    标签: api date jodatime


    【解决方案1】:

    代码中有错误。日期格式说明符需要是“dd”而不是 DD。这解决了错误

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 2021-10-07
      • 2011-01-26
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 2017-02-19
      相关资源
      最近更新 更多