Get date representation in seconds?

Sam picture Sam · Jun 19, 2012 · Viewed 41.5k times · Source

I am using an API which requires a date parameter as a number of seconds, an int.

My problem is that I currently store this time in java.util.date and I was wondering if there is some way to convert the java.util.date variable to seconds so that I can fit it into the int parameter which the API requires?

Answer

Karthik Kumar Viswanathan picture Karthik Kumar Viswanathan · Jun 19, 2012

import java.util.Date;

... long secs = (new Date().getTime())/1000; ...

Please see - http://docs.oracle.com/javase/6/docs/api/java/util/Date.html#getTime()