0

I am trying to get the difference between the current time and the time which is stored in DB. I tried below code but its not giving the correct result since java.lang.System.currentTimeMillis() returns UTC time whereas we save date to our application in EST format.

Code :

java.lang.System.currentTimeMillis() - emp.getModifiedDate()

Could you please suggest me a correct way to do it ?It will be very helpful for me

Thanks

Rahman
  • 3,755
  • 3
  • 26
  • 43

2 Answers2

0

System.currentTimeMillis() returns the number of milliseconds since January 1, 1970 UTC. It is time-zone agnostic.

If you are storing the time in the database as millis then simply subtracting the two numbers will give you the correct difference.

If you are storing the time in some other way (e.g. as a String), you can parse it (e.g. with SimpleDateFormat) to take into account the time zone and then obtain a Date from which you can obtain the millis.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

Hope this helps! This will give you difference in no of days.

Date date1 = new Date();   
long diff = date1.getTime() - emp.getModifiedDate().getTime();           
System.out.println((int) (diff) / (1000 * 60 * 60 * 24)));

For more read this

Rajat Khandelwal
  • 477
  • 1
  • 5
  • 19