Skip to content

Commit d13d801

Browse files
authored
Update 2021-05-13-how-to-get-current-timestamps-in-java.md
1 parent b98f88a commit d13d801

File tree

1 file changed

+68
-86
lines changed

1 file changed

+68
-86
lines changed

‎_posts/java-time/2021-05-13-how-to-get-current-timestamps-in-java.md

+68-86
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
---
1+
---
22
layout: post
33
title: "How To Get Current Timestamp In Java?"
44
author: gaurav
@@ -14,7 +14,7 @@ We check both [`java.time.Instant`](https://docs.oracle.com/javase/8/docs/api/ja
1414

1515
## 1. Using `java.time.Instant` ( for Java 8)
1616
As per the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html),
17-
> This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application.
17+
> This class models a single instantaneous point on the timeline. This might be used to record event timestamps in the application.
1818
1919
I have given an example below that shows how to get the current timestamp using `java.time.Instant`
2020

@@ -24,47 +24,40 @@ import java.time.Instant;
2424
import java.util.Date;
2525

2626
/**
27-
* A Java Program to get current timestamp using using the java.time.Instant
28-
*
29-
* @author Gaurav Kukade at coderolls.com
30-
*
27+
* A Java Program to get the current timestamp using the java.time.Instant
28+
* @author coderolls.com
3129
*/
32-
public class InstantExample {
33-
34-
public static void main(String[] args) {
35-
36-
Instant instant = Instant.now();
37-
System.out.println("1. instant: "+ instant); // 2021-05-13T15:05:18.734Z
38-
39-
40-
//convert date object to instant
41-
Date date = new Date();
42-
Instant instantFromDate = date.toInstant();
43-
44-
System.out.println("2. instantFromDate: " +instantFromDate); // 2021-05-13T15:05:18.873Z
45-
46-
47-
//get instnat from the java.sql.Timestamp
48-
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
49-
Instant instantFromTimestamp = timestamp.toInstant();
50-
51-
System.out.println("3. instantFromTimestamp: "+ instantFromTimestamp); // 2021-05-13T15:05:18.874Z
52-
53-
//get the epoch timestamp in milliseconds
54-
//epoch timestamp is number of milliseconds since January 1, 1970, 00:00:00 GMT
55-
Instant instant2 = Instant.now();
56-
long timeStampMillis = instant2.toEpochMilli();
57-
58-
System.out.println("4. timeStampMillis: "+ timeStampMillis); // 1620918318874
59-
60-
//get the epoch timestamp in seconds
61-
Instant instant3 = Instant.now();
62-
long timeStampSeconds = instant3.getEpochSecond();
63-
64-
System.out.println("5. timeStampSeconds: "+ timeStampSeconds); // 1620918318
65-
66-
}
67-
30+
public class InstantExample {
31+
32+
public static void main(String[] args) {
33+
Instant instant = Instant.now();
34+
System.out.println("1. instant: "+ instant); // 2021-05-13T15:05:18.734Z
35+
36+
//convert date object to instant
37+
Date date = new Date();
38+
Instant instantFromDate = date.toInstant();
39+
40+
System.out.println("2. instantFromDate: " +instantFromDate); // 2021-05-13T15:05:18.873Z
41+
42+
//get instant from the java.sql.Timestamp
43+
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
44+
Instant instantFromTimestamp = timestamp.toInstant();
45+
46+
System.out.println("3. instantFromTimestamp: "+ instantFromTimestamp); // 2021-05-13T15:05:18.874Z
47+
48+
//get the epoch timestamp in milliseconds
49+
//epoch timestamp is the number of milliseconds since January 1, 1970, 00:00:00 GMT
50+
Instant instant2 = Instant.now();
51+
long timeStampMillis = instant2.toEpochMilli();
52+
53+
System.out.println("4. timeStampMillis: "+ timeStampMillis); // 1620918318874
54+
55+
//get the epoch timestamp in seconds
56+
Instant instant3 = Instant.now();
57+
long timeStampSeconds = instant3.getEpochSecond();
58+
59+
System.out.println("5. timeStampSeconds: "+ timeStampSeconds); // 1620918318
60+
}
6861
}
6962
```
7063
Output:
@@ -75,7 +68,7 @@ Output:
7568
4. timeStampMillis: 1620919459988
7669
5. timeStampSeconds: 1620919459
7770
```
78-
See above code as [GitHub Gist](https://gist.github.com/gauravkukade/b1ceabac137864efd7258b60610cd9dd)
71+
See the above code as [GitHub Gist](https://gist.github.com/gauravkukade/b1ceabac137864efd7258b60610cd9dd)
7972

8073
### Explanation:
8174

@@ -93,25 +86,20 @@ import java.util.Date;
9386

9487
/**
9588
* A Java program to get the current timestamp using java.sql.Timestamp
96-
*
97-
* @author Gaurav Kukade at coderolls.com
98-
*
89+
* @author coderolls.com
9990
*/
10091
public class TimestampExample {
10192

102-
public static void main(String[] args) {
103-
104-
//using system time in milliseconds
105-
Timestamp timestampFromSystemTime = new Timestamp(System.currentTimeMillis());
106-
System.out.println("1. timestampFromSystemTime: "+timestampFromSystemTime); // 2021-05-13 20:55:44.179
107-
108-
//using java.util.Date
109-
Date date = new Date();
110-
Timestamp timestampFromDateObject = new Timestamp(date.getTime());
111-
System.out.println("2. timestampFromDateObject: " +timestampFromDateObject); // 2021-05-13 20:55:44.187
112-
113-
}
114-
93+
public static void main(String[] args) {
94+
//using system time in milliseconds
95+
Timestamp timestampFromSystemTime = new Timestamp(System.currentTimeMillis());
96+
System.out.println("1. timestampFromSystemTime: "+timestampFromSystemTime); // 2021-05-13 20:55:44.179
97+
98+
//using java.util.Date
99+
Date date = new Date();
100+
Timestamp timestampFromDateObject = new Timestamp(date.getTime());
101+
System.out.println("2. timestampFromDateObject: " +timestampFromDateObject); // 2021-05-13 20:55:44.187
102+
}
115103
}
116104
```
117105
Output:
@@ -121,12 +109,12 @@ Output:
121109
2. timestampFromDateObject: 2021-05-13 20:55:44.187
122110
```
123111

124-
See above code as [GitHub Gist](https://gist.github.com/gauravkukade/a965f1a078d382ff7366e7ba6d13561c)
112+
See the above code as [GitHub Gist](https://gist.github.com/gauravkukade/a965f1a078d382ff7366e7ba6d13561c)
125113

126114
### Explanation:
127115

128-
1. In the first case, we are got the `timestamp` using the system time in milliseconds. We can get the system time in millisecond using the `System.currentTimeMillis()`.
129-
2. In the second case, we have created a new `date` object, and then we have used the `.getTime()` on the `date` object to get the current time in milliseconds. We have passed the time milliseconds to timestamp constructor to get the current timestamp.
116+
1. In the first case, we get the `timestamp` using the system time in milliseconds. We can get the system time in milliseconds using the `System.currentTimeMillis()`.
117+
2. In the second case, we have created a new `date` object, and then we have used the `.getTime()` on the `date` object to get the current time in milliseconds. We have passed the time milliseconds to the timestamp constructor to get the current timestamp.
130118

131119
Sometimes we may need to convert the `java.time.Instant` to `java.sql.Timestamp` or vice versa. So let's see both conversions below.
132120

@@ -146,15 +134,12 @@ import java.time.Instant;
146134
*/
147135
public class InstantToTimestamp {
148136

149-
public static void main(String[] args) {
150-
151-
// get Timestamp from Instant
152-
Instant instant = Instant.now();
153-
Timestamp timestamp = Timestamp.from(instant);
154-
System.out.println("Instant To Timestamp: " +timestamp); // 2021-05-13 21:07:44.381
155-
156-
157-
}
137+
public static void main(String[] args) {
138+
//Get Timestamp from Instant
139+
Instant instant = Instant.now();
140+
Timestamp timestamp = Timestamp.from(instant);
141+
System.out.println("Instant To Timestamp: " +timestamp); // 2021-05-13 21:07:44.381
142+
}
158143

159144
}
160145
```
@@ -163,7 +148,7 @@ Output:
163148
```
164149
Instant To Timestamp: 2021-05-13 21:07:44.381
165150
```
166-
See above code as [GitHub Gist](https://gist.github.com/gauravkukade/ea3bc951f15be0010569d306ccafa975)
151+
See the above code as [GitHub Gist](https://gist.github.com/gauravkukade/ea3bc951f15be0010569d306ccafa975)
167152

168153
### Explanation:
169154

@@ -190,31 +175,28 @@ import java.time.Instant;
190175
*/
191176
public class TimestampToInstant {
192177

193-
public static void main(String[] args) {
194-
195-
//get instnat from the java.sql.Timestamp
196-
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
197-
Instant instant = timestamp.toInstant();
198-
System.out.println("Timestamp To Instant: " +instant); // 2021-05-13T15:39:08.046Z
199-
200-
}
201-
178+
public static void main(String[] args) {
179+
//get instant from the java.sql.Timestamp
180+
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
181+
Instant instant = timestamp.toInstant();
182+
System.out.println("Timestamp To Instant: " +instant); // 2021-05-13T15:39:08.046Z
183+
}
202184
}
203185
```
204186

205187
Output:
206188
```
207189
Timestamp To Instant: 2021-05-13T15:39:08.046Z
208190
```
209-
See above code as [GitHub Gist](https://gist.github.com/gauravkukade/a031b1904d4efaf758a59462ee2dc02e)
191+
See the above code as [GitHub Gist](https://gist.github.com/gauravkukade/a031b1904d4efaf758a59462ee2dc02e)
210192

211193
### Explanation:
212194

213195
In the above example, we have used the `.toInstant()` method on the `timestamp` object to get the current timestamp (as Instant ).
214196

215197
## Conclusion
216198

217-
We can get the current timestamp in java using the `Instant` and `Timestamp`.
199+
We can get the current timestamp in Java using the `Instant` and `Timestamp`.
218200

219201
From Java 8, it is recommended to use the timestamp of the Instant class. We can get the current timestamp from `Instant`as is given below,
220202

@@ -225,13 +207,13 @@ Instant instant = Instant.now();
225207
Date date = new Date();
226208
Instant instantFromDate = date.toInstant();
227209

228-
//get instnat from the java.sql.Timestamp
210+
//get instant from the java.sql.Timestamp
229211
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
230212
Instant instantFromTimestamp = timestamp.toInstant();
231213

232214

233215
//get the epoch timestamp in milliseconds
234-
//epoch timestamp is number of milliseconds since January 1, 1970, 00:00:00 GMT
216+
//epoch timestamp is the number of milliseconds since January 1, 1970, 00:00:00 GMT
235217
Instant instant2 = Instant.now();
236218
long timeStampMillis = instant2.toEpochMilli();
237219

@@ -253,7 +235,7 @@ System.out.println(date.getTime());
253235
Timestamp timestampFromDateObject = new Timestamp(date.getTime());
254236

255237
```
256-
Also we can convert the `java.time.Instant` to `java.sql.Timestamp` or vice versa.
238+
Also, we can convert the `java.time.Instant` to `java.sql.Timestamp` or vice versa.
257239

258240

259241
**Instant To Timestamp**

0 commit comments

Comments
 (0)