1
- ---
1
+ ---
2
2
layout : post
3
3
title : " How To Get Current Timestamp In Java?"
4
4
author : gaurav
@@ -14,7 +14,7 @@ We check both [`java.time.Instant`](https://docs.oracle.com/javase/8/docs/api/ja
14
14
15
15
## 1. Using ` java.time.Instant ` ( for Java 8)
16
16
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.
18
18
19
19
I have given an example below that shows how to get the current timestamp using ` java.time.Instant `
20
20
@@ -24,47 +24,40 @@ import java.time.Instant;
24
24
import java.util.Date ;
25
25
26
26
/**
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
31
29
*/
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
+ }
68
61
}
69
62
```
70
63
Output:
@@ -75,7 +68,7 @@ Output:
75
68
4. timeStampMillis: 1620919459988
76
69
5. timeStampSeconds: 1620919459
77
70
```
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 )
79
72
80
73
### Explanation:
81
74
@@ -93,25 +86,20 @@ import java.util.Date;
93
86
94
87
/**
95
88
* 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
99
90
*/
100
91
public class TimestampExample {
101
92
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
+ }
115
103
}
116
104
```
117
105
Output:
@@ -121,12 +109,12 @@ Output:
121
109
2. timestampFromDateObject: 2021-05-13 20:55:44.187
122
110
```
123
111
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 )
125
113
126
114
### Explanation:
127
115
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.
130
118
131
119
Sometimes we may need to convert the ` java.time.Instant ` to ` java.sql.Timestamp ` or vice versa. So let's see both conversions below.
132
120
@@ -146,15 +134,12 @@ import java.time.Instant;
146
134
*/
147
135
public class InstantToTimestamp {
148
136
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
+ }
158
143
159
144
}
160
145
```
@@ -163,7 +148,7 @@ Output:
163
148
```
164
149
Instant To Timestamp: 2021-05-13 21:07:44.381
165
150
```
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 )
167
152
168
153
### Explanation:
169
154
@@ -190,31 +175,28 @@ import java.time.Instant;
190
175
*/
191
176
public class TimestampToInstant {
192
177
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
+ }
202
184
}
203
185
```
204
186
205
187
Output:
206
188
```
207
189
Timestamp To Instant: 2021-05-13T15:39:08.046Z
208
190
```
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 )
210
192
211
193
### Explanation:
212
194
213
195
In the above example, we have used the ` .toInstant() ` method on the ` timestamp ` object to get the current timestamp (as Instant ).
214
196
215
197
## Conclusion
216
198
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 ` .
218
200
219
201
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,
220
202
@@ -225,13 +207,13 @@ Instant instant = Instant.now();
225
207
Date date = new Date ();
226
208
Instant instantFromDate = date. toInstant();
227
209
228
- // get instnat from the java.sql.Timestamp
210
+ // get instant from the java.sql.Timestamp
229
211
Timestamp timestamp = new Timestamp (System . currentTimeMillis());
230
212
Instant instantFromTimestamp = timestamp. toInstant();
231
213
232
214
233
215
// 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
235
217
Instant instant2 = Instant . now();
236
218
long timeStampMillis = instant2. toEpochMilli();
237
219
@@ -253,7 +235,7 @@ System.out.println(date.getTime());
253
235
Timestamp timestampFromDateObject = new Timestamp (date. getTime());
254
236
255
237
```
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.
257
239
258
240
259
241
** Instant To Timestamp**
0 commit comments