Convert Current date to integer
tl;dr
Instant.now().getEpochSecond() // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
Details
As others stated, a 32-bit integer cannot hold a number big enough for the number of seconds from the epoch (beginning of 1970 in UTC) and now. You need 64-bit integer (a long
primitive or Long
object).
java.time
The other answers are using old legacy date-time classes. They have been supplanted by the java.time classes.
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant now = instant.now() ;
You can interrogate for the number of milliseconds since the epoch. Beware this means a loss of data, truncating nanoseconds to milliseconds.
long millisecondsSinceEpoch = now.toEpochMilli() ;
If you want a count of nanoseconds since epoch, you will need to do a bit of math as the class oddly lacks a toEpochNano
method. Note the L
appended to the billion to provoke the calculation as 64-bit long integers.
long nanosecondsSinceEpoch = ( instant.getEpochSecond() * 1_000_000_000L ) + instant.getNano() ;
Whole seconds since epoch
But the end of the Question asks for a 10-digit number. I suspect that means a count of whole seconds since the epoch of 1970-01-01T00:00:00. This is commonly referred to as Unix Time or Posix Time.
We can interrogate the Instant
for this number. Again, this means a loss of data with the truncation of any fraction-of-second this object may hold.
long secondsSinceEpoch = now.getEpochSecond() ; // The number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
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, Java SE 10, Java SE 11, and later - 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
- Most 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.
The issue is that an Integer is not large enough to store a current date, you need to use a Long.
The date is stored internally as the number of milliseconds since 1/1/1970.
The maximum Integer value is 2147483648, whereas the number of milliseconds since 1970 is currently in the order of 1345618537869
Putting the maximum integer value into a date yields Monday 26th January 1970.
Edit: Code to display division by 1000 as per comment below:
int i = (int) (new Date().getTime()/1000);
System.out.println("Integer : " + i);
System.out.println("Long : "+ new Date().getTime());
System.out.println("Long date : " + new Date(new Date().getTime()));
System.out.println("Int Date : " + new Date(((long)i)*1000L));
Integer : 1345619256
Long : 1345619256308
Long date : Wed Aug 22 16:37:36 CST 2012
Int Date : Wed Aug 22 16:37:36 CST 2012
In order to get current date as integer(10 digit number), you need to divide the long returned from new Date().getTime() by 1000.
This will be in int range and is good until 18 Jan 2038.