13

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.

Pi Horse
  • 2,350
  • 8
  • 30
  • 51
  • 2
    [It's in the Javadoc](http://joda-time.sourceforge.net/apidocs/org/joda/time/base/BaseDateTime.html#getMillis%28%29) – fvu Jan 18 '14 at 01:21

4 Answers4

30

The other answers are correct.

Four ways to count-from-epoch

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.

java.time

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

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.


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.

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.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
22

If you mean current or now then System.currentTimeMillis() will do it for you. More info here: http://docs.oracle.com/javase/7/docs/api/java/lang/System.html

yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65
6

Assuming you're using joda-time, the following line will sort you:

datetime.getMillis()
hd1
  • 33,938
  • 5
  • 80
  • 91
1

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.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249