How to convert a Java Date to a Long value

If you ever need to convert a Java Date to a Long, just call the getTime method on the Date instance. This Scala example shows the process:

scala> import java.util.Calendar
import java.util.Calendar

scala> val d = Calendar.getInstance.getTime
d: java.util.Date = Tue Jan 16 19:34:37 MST 2018

scala> d.getTime
res0: Long = 1516156477659

Even though that code is written in Scala, as you can see, it’s easy to convert that code to Java. If you ever need to convert a Java Date to a Long value (technically a long in Java), I hope this example is helpful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.