-6

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.

Aakanksha Choudhary
  • 515
  • 2
  • 5
  • 19
  • 1
    I recommend you avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 08 '18 at 20:38
  • Yes, I have eliminated the SimpleDateFormat now! Thanks @OleV.V. – Aakanksha Choudhary Aug 08 '18 at 21:09

2 Answers2

2

The time difference (in milliseconds) can be obtained using:

ChronoUnit.MILLIS
   .between(Instant.parse("2018-07-26T16:00:17.163Z"), Instant.now())

Instant.now() will be the current UTC time, and your input date is UTC time, which makes the diff sensible. Regardless of the current time zone, the difference will be consistent.
You can change the time unit accordingly (such as ChronoUnit.HOURS).

ernest_k
  • 44,416
  • 5
  • 53
  • 99
-1

You can calculate the difference like the following.

// Your date string
val dateStr    = "2018-07-26T16:00:17.163Z"
// Define the date format
val dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")    
// Set Chicago timezone    
dateFormat.setTimeZone(java.util.TimeZone.getTimeZone("America/Chicago"))

// Calculate the diff
val diff: Long = dateFormat.parse(dateStr).getTime() - System.currentTimeMillis()

This code uses only Java standard library.

abc
  • 19
  • 3
  • Incorrect answer. The `Z` in the string is an offset from UTC, so don’t parse it as a literal. It means offset zero, so the time zone of your formatter, America/Chicago, disagrees and gives incorrect results. PS Provided you are using Java 8, 9, 10 or 11 the other answert also only uses the standard Java library. – Ole V.V. Aug 08 '18 at 20:41