tl;dr
Instant.now().getEpochMilli()
java.time
Other Answers use the terrible date-time classes (Calendar
, Date
) that are supplanted by the modern java.time classes built into Java 8 and later.
For a Jakarta EE web app, you are executing Java on the web server, not the web browser client. So running code to ask the current moment will give you the current moment on the server.
Capture the current moment as seen in UTC.
Instant instant = Instant.now() ;
Most of your work should be done in UTC. For presentation to the user, localize to a time zone of their choice.
ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = instant.atZone( z ) ;
You said:
What I want is eg: Current server time is 9/25/2012:10:19AM in milliseconds.
To get a count of milliseconds since the epoch reference of first moment of 1970 in UTC (1970-01-01T00:00Z), interrogate the Instant
. Beware of data loss, as the Instant
may contain microseconds or nanoseconds in its fractional second.
long secondsSinceEpoch = Instant.now().getEpochMilli() ;

About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?