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:
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:
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:
The output from the author’s computer is:
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:
The output of the author’s computer is:
The current timestamp object must be obtained first before the number of seconds is subtracted. The principal method used here is:
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:
The output of the author’s computer is:
The current timestamp object has to be obtained first before the number of seconds is added to it. The principal method used here is:
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,
The following program displays EPOCH:
The output is,
as expected.
MAX gives the maximum supported Instant. The following program outputs the value for MAX in string form:
The output is:
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:
The output is:
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().