2016-09-05 5 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" 
    } 

Das ist mir eine Ausnahme gibt:mit JDateTime Arbeiten gibt mir eine Ausnahme

java.lang.NumberFormatException: Für die Eingabe-Stichwort: "02-2100"

Ich bin mit JDateTime 2.9.4 mit diesem sbt: ("joda-time" % "joda-time" % "2.9.4")

+1

Auf welcher Linie? Was sind die Methodenparameterwerte? –

+0

In der Zeile..val Start = neue JDateTime ("2016-02-21 00: 17: 43: 126", "JJJJ-MM-TT HH: mm: SS: SSS") .. unabhängig von Parametern .. – Luckylukee

Antwort

0

Vor diesem sbt.build:

Diese funktionieren würde (was nur minimale Änderungen an Ihrem Code):

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" 

} 

Verbrauch:

funcTimeDiffernceInSeconds("2016-02-21 00:17:43:126", "2016-02-22 00:17:43:126") 
res0: String = 86400 
Verwandte Themen