myhusky
myhusky

Reputation: 90

What's the equivlent of PHP time() in Java?

I need to store the current time as long integer into a database table, there are maybe better ways to store date/time in database, but here long integer is the requirement.

In PHP, I can get the current time in integer with time(), in Java, is there any function or simple code that do the same trick?

Thank you

Upvotes: 0

Views: 5107

Answers (2)

Greg Shackles
Greg Shackles

Reputation: 10139

Sounds like java.util.Date.getTime() is what you're looking for. The PHP time() function is in seconds though, and the Java function is in milliseconds so you'll want to do a conversion depending on the unit you need.

Upvotes: 0

Amir Afghani
Amir Afghani

Reputation: 38531

You can do System.currentTimeMillis() which returns a long.

From the JavaDoc:

Returns the current time in milliseconds. Note that while the unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds. See the description of the class Date for a discussion of slight discrepancies that may arise between "computer time" and coordinated universal time (UTC)

Upvotes: 8

Related Questions