I want to convert the current datetime to a long value in Java.
I know it has to be something with the DateTime
DateTime datetime = new DateTime();
But I couldn't find a way to convert the dateTime to long.
You can call System. currentTimeMillis(), which returns a long . Or, you can call BaseTime. getMillis().
final SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss a"); new Thread(new Runnable() { @Override public void run() { while (true) { System. out. println(format. format(new Date())); try { Thread.
The other answers are correct.
To spell it out in detail, here are four ways to get the number of milliseconds since the Unix Epoch, the beginning of January 1, 1970 in UTC/GMT (no time zone offset). Presented in order of preference.
The java.time classes built into Java 8 and later is defined by JSR 310. These classes supplant the outmoded java.util.Date/Calendar classes. These classes are inspired by Joda-Time but are re-architected.
The java.time classes are capable of holding nanosecond resolution. In Java 8 specifically, capturing the current moment is limited to milliseconds, due to an old implementation of Clock
. Java 9 brought a fresh implementation of Clock
capable of capturing the current moment in finer resolution, limited by the capability of the host OS and host hardware. On Oracle JDK 9.0.4 on macOS Sierra, I get values in microseconds, for six digits of decimal fraction.
The Instant
class includes a convenience method toEpochMilli
provides milliseconds. Beware of data loss, as any microseconds or nanoseconds in the Instant
are ignored when generating a count of milliseconds.
long millis = Instant.now().toEpochMilli();
System
The System
class’ method currentTimeMillis works. But chances are that you are doing some other work with date-time values. If so, skip System and just use the date-time classes.
long millis = System.currentTimeMillis();
Joda-Time is the popular third-party open-source library used in place of java.util.Date
/Calendar
. Its main class is DateTime
, so perhaps this is what you had in mind given your question's use of that term.
The Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes.
long millis = org.joda.time.DateTime.now().getMillis();
java.util.Date
This class is best avoided, along with its siblings java.util.Calendar
and java.text.SimpleDateFormat
. The troublesome old date-time classes are now legacy, supplanted by java.time classes. But for your specific purpose, this class does work.
long millis = new java.util.Date().getTime();
By the way, I do not recommend tracking time as a count-from-epoch. Makes debugging difficult, and near impossible to spot invalid data as humans cannot interpret a long
as a date-time. Also, ambiguous as different systems use different epoch reference dates, and use different granularities of whole seconds, milliseconds, microseconds, nanoseconds, and others.
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
You can call System.currentTimeMillis(), which returns a long
. Or, you can call BaseTime.getMillis(). Either will give you the date and time as a long
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With