2016-09-05 37 views
0
val funcTimeDiffernceInSeconds = (startTime: String, endTime: String) => { 

    println("starttime is"+startTime+"endtime"+endTime) 

    if (startTime.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+") && endTime.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+")) { 
     //val start = new JDateTime(startTime, "yyyy-MM-dd HH:mm:ss:SSS") 

     val start = new JDateTime("2016-02-21 00:17:43:126", "yyyy-MM-dd HH:mm:ss:SSS") 

     val end = new JDateTime(endTime, "yyyy-MM-dd HH:mm:ss:SSS") 
     new Period(start, end).getSeconds().toString() 
    } 
    else 
     "Invalid" 
    } 

這給了我一個例外:與JDateTime工作給了我一個例外

java.lang.NumberFormatException:對於輸入字符串: 「02-2100」

我使用JDateTime使用該SBT 2.9.4:("joda-time" % "joda-time" % "2.9.4")

+1

在哪條線路?什麼是方法參數值? –

+0

On the line..val start = new JDateTime(「2016-02-21 00:17:43:126」,「yyyy-MM-dd HH:mm:ss:SSS」)..不管參數。 – Luckylukee

回答

0

有了這個sbt.build:

// https://mvnrepository.com/artifact/joda-time/joda-time 
libraryDependencies += "joda-time" % "joda-time" % "2.9.4" 

// https://mvnrepository.com/artifact/org.joda/joda-convert 
libraryDependencies += "org.joda" % "joda-convert" % "1.8.1" 

這會工作(做最小的改動你的代碼):

import org.joda.time.Duration 
import org.joda.time.format.DateTimeFormat 

val funcTimeDiffernceInSeconds = (startTime: String, endTime: String) => { 

    //println("starttime is "+startTime+"\nendtime "+endTime) 

    if (startTime.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+") && endTime.matches("[\\d]{4}-[\\d]{2}-[\\d]{2}[\\s]{1}[\\d]{2}:[\\d]{2}:[\\d]{2}:[\\d]+")) { 
    //val start = new JDateTime(startTime, "yyyy-MM-dd HH:mm:ss:SSS") 

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

    val start = formatter.parseDateTime(startTime) 

    val end = formatter.parseDateTime(endTime) 

    new Duration(start, end).getStandardSeconds.toString 
    } 
    else 
    "Invalid" 

} 

用法:

funcTimeDiffernceInSeconds("2016-02-21 00:17:43:126", "2016-02-22 00:17:43:126") 
res0: String = 86400 
相關問題