Java

Get Current Timestamp in Java

The timestamp is the date and time when an event occurred. In computers, it can be the date-time when a special input was made or when a special output was made, or some other internal computer event, such as inserting particular data into a database. The current timestamp is the timestamp now. If an event occurs now, and the timestamp is recorded, tomorrow it would be the timestamp of yesterday.

There is a local timestamp, GMT timestamp, and UTC timestamp. The local timestamp is the date-time used in your country for legal, commercial, and social purposes. GMT timestamp and UTC timestamp have the same reference. A time zone is an offset for your country that can be added to UTC to have your local time. A time zone of +08:45 means adding 8 hours and 45 minutes to the UTC time.

The date part of the timestamp has the year, month, and day of the month. The time part of the timestamp has at least the hour, the minutes, and the seconds. It can also have milliseconds and/or the time zone (offset). Note: 1 ms = 1/1000 s.

The timestamp should be given by the computer. So the system clock has to be correct. The class, java.time.Instant is what should be used to obtain the timestamp in Java today.

java.time.Instant

Java has the class, java.time.Instant, which has been designed purposely for the timestamp. The Instant class has two important methods: now () and toString(). “Now” returns the timestamp object now. toString() returns the string format of the timestamp. The following program illustrates their uses:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant tsObj = Instant.now();

String tsStr = tsObj.toString();

System.out.println(tsStr);

}

}

The program begins with the importation of the Instant class of java.time package. Then there is the definition (implementation) of the main class. Inside the main() method, the first statement uses the now() method of the Instant class. now() is a static method, meaning it does not need the instantiated object in order to operate. It uses the Instant class name to return the timestamp object of its Instant class. The toString() method of the timestamp Instant object returns the readable string of the timestamp object in ISO-8601 format.

The output of the author’s computer is:

2021-10-31T11:53:15.271438Z

This needs explanation. The first four digits are the year of the event. This is followed by a hyphen, which separates it from the next two digits, which is the month of the event. This is followed by a hyphen, which separates it from the next two digits, which is the day of the month of the event. A timestamp has the date part and the time part. The date and time parts are separated with ‘T’. The time part begins with two digits (24-hour clock), which is the hour of the event. This is followed by a colon, which separates it from the next two digits, which are the minutes of the event. This is followed by a colon, which separates it from the next two digits, which are the seconds of the event. Then there is the dot and microseconds. Z means that the timestamp has no offset.

Note: toString() is not a static method. It needs an instantiated Instant object in order to operate. It returns a string, which can be stored (in a database).

Java Epoch

Time in Java is actually measured from UNIX Epoch. UNIX Epoch is 1970-01-01T00:00:00Z .

Number of seconds from Java Epoch

The following program gets the number of seconds now, since Java Epoch:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant tsObj = Instant.now();

long secs = tsObj.getEpochSecond();

System.out.println(secs);

}

}

The output from the author’s computer is:

1635687372

The method of the Instant object used is getEpochSecond(). It returns a long integer.

Instant minusSeconds(long secondsToSubtract)

The current date-time can be returned with a number of seconds subtracted. This will give datetime before now. The following program illustrates this:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant instObj = Instant.now();

Instant sbObj = instObj.minusSeconds(1000000000);

String str = sbObj.toString();

System.out.println(str);

}

}

The output of the author’s computer is:

1990-02-22T12:07:28.777609Z

The current timestamp object must be obtained first before the number of seconds is subtracted. The principal method used here is:

minusSeconds(long secondsToSubtract)

It works with an instant object and not the instant class (name).

Instant plusSeconds(long secondsToAdd)

The current date-time can be returned with a number of seconds added. This will give datetime after now. The following program illustrates this:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant instObj = Instant.now();

Instant sbObj = instObj.plusSeconds(1000000000);

String str = sbObj.toString();

System.out.println(str);

}

}

The output of the author’s computer is:

2053-07-09T15:52:24.919626Z

The current timestamp object has to be obtained first before the number of seconds is added to it. The principal method used here is:

plusSeconds(long secondsToAdd)

It works with an instant object and not the instant class (name).

Static Properties of the Instant Class

The static properties (fields) of the Instant class are EPOCH, MAX, and MIN. A static property does not need the instantiated object for access. It works with the class name. EPOCH is,

1970-01-01T00:00:00Z

The following program displays EPOCH:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant instObj = Instant.EPOCH;

String str = instObj.toString();

System.out.println(str);

}

}

The output is,

1970-01-01T00:00:00Z

as expected.

MAX gives the maximum supported Instant. The following program outputs the value for MAX in string form:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant instObj = Instant.MAX;

String str = instObj.toString();

System.out.println(str);

}

}

The output is:

+1000000000-12-31T23:59:59.999999999Z

The last nine-digit number is for nanoseconds.

MIN gives the minimum supported Instant. The following program outputs the value for MIN in string form:

import java.time.Instant;

public class TheClass {

public static void main(String[] args) {

Instant instObj = Instant.MIN;

String str = instObj.toString();

System.out.println(str);

}

}

The output is:

-1000000000-01-01T00:00:00Z

Conclusion

The timestamp is the date and time when an event occurred. The current timestamp is the timestamp now. The Instant class of the Java package, java.time.* should be used for timestamp work. Important methods of the timestamp class are now() and toString().

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.