Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert current datetime to long in Java [closed]

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.

like image 876
Pi Horse Avatar asked Jan 18 '14 01:01

Pi Horse


People also ask

How can I get current date in long time?

You can call System. currentTimeMillis(), which returns a long . Or, you can call BaseTime. getMillis().

How do you change current time in Java?

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.


2 Answers

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?

  • Java SE 8, Java SE 9, and later
    • Built-in.
    • Part of the standard Java API with a bundled implementation.
    • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
    • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
    • Later versions of Android bundle implementations of the java.time classes.
    • For earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….

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.

like image 125
Basil Bourque Avatar answered Sep 30 '22 11:09

Basil Bourque


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.

like image 20
Elliott Frisch Avatar answered Sep 30 '22 10:09

Elliott Frisch