I am referring to this question and trying to get time difference between a given time and current time in Java. My input time is in the format 2018-07-26T16:00:17.163Z
and I want it's time difference with my current time in Chicago. How do I do that? Here is my snippet
val currTime = System.currentTimeMillis()
var strDate = timeStampToDateComverter(currTime)
import java.text.SimpleDateFormat
import java.util.Calendar
val formatter_datetime = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
import java.util.TimeZone
formatter_datetime.setTimeZone(TimeZone.getTimeZone("America/Chicago"))
val parsedTime2 = formatter_datetime.parse(strDate)
var str1 = "2018-07-26T16:00:17.163Z"
val parsedTime1 = formatter_datetime.parse((str1.substring(0,str1.indexOfSlice("T")) + " " + str1.substring(str1.indexOfSlice("T")+1,str1.indexOfSlice("T")+9)))
val dm = TimeUnit.MILLISECONDS.toMillis(new java.sql.Date(parsedTime2.getTime()).getTime()) - TimeUnit.MILLISECONDS.toMillis(new java.sql.Date(parsedTime1.getTime()).getTime())
val seconds = dm / 1000
val minutes = seconds / 60
val hours = minutes / 60
val days = hours / 24
val weeks = days / 7
val days_out = days % 7
pipelineTotalExecutionTime = (weeks +"w "+ days_out + "d " + hours % 24 + "h " + minutes % 60 + "m")
Here is my timeStampToDateComverter
def timeStampToDateComverter(i : Long) : String = {
val dt = new Date(i)
val sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss")
sfd.format(dt)
}
A code snippet would be highly appreciated. Thank you.