Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Api Joda DateTime库显示不正确的结果_Api_Date_Jodatime - Fatal编程技术网

Api Joda DateTime库显示不正确的结果

Api Joda DateTime库显示不正确的结果,api,date,jodatime,Api,Date,Jodatime,JodaAPI似乎返回了错误的结果。我有两个日期要比较。其中一种格式为YYYY-MM-DD HH:MM:ss,另一种格式为YYYYMMDD。阅读了关于前者不是标准格式的帖子后,我将其转换为与后者相同的格式(YYYYMMDD)。然而,结果仍然令人无法接受。我尝试了DateTimeFormatterBuilder,还手动将日期替换为所需格式。下面是我的代码。我知道daysBetween会对一个月的天数产生影响,可能我应该使用其他API来实际获得差异。但是,在本例中,底线是日期1大于日期2,这是错误的

JodaAPI似乎返回了错误的结果。我有两个日期要比较。其中一种格式为YYYY-MM-DD HH:MM:ss,另一种格式为YYYYMMDD。阅读了关于前者不是标准格式的帖子后,我将其转换为与后者相同的格式(YYYYMMDD)。然而,结果仍然令人无法接受。我尝试了DateTimeFormatterBuilder,还手动将日期替换为所需格式。下面是我的代码。我知道daysBetween会对一个月的天数产生影响,可能我应该使用其他API来实际获得差异。但是,在本例中,底线是日期1大于日期2,这是错误的。 仅供参考,我将nscala时间用作scala代码的包装器。它在内部调用jodaapi。我的代码有什么问题吗?还是库中有bug

注:当年份不同时,结果是正确的。因此,比较似乎只是比较年份部分

代码如下:

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

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