|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "How To Get Current Timestamp In Java?" |
| 4 | +author: gaurav |
| 5 | +categories: [Java, Java Time] |
| 6 | +description: "In this article, we will see how to get the current timestamp in Java." |
| 7 | +--- |
| 8 | +In this article, we will see how to get the current timestamp in Java. |
| 9 | + |
| 10 | +To get the current timestamp in Java, we can use [`java.time.Instant`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html) from Java 8 and [`java.sql.Timestamp`](https://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html) till Java 7. |
| 11 | + |
| 12 | +We check both [`java.time.Instant`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html) and [`java.sql.Timestamp`](https://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html) one by one. |
| 13 | + |
| 14 | +## 1. Using `java.time.Instant` ( for Java 8) |
| 15 | +As per the [javadoc](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html), |
| 16 | +> This class models a single instantaneous point on the time-line. This might be used to record event time-stamps in the application. |
| 17 | +
|
| 18 | +I have given an example below that shows how to get the current timestamp using `java.time.Instant` |
| 19 | + |
| 20 | +```java |
| 21 | +import java.sql.Timestamp; |
| 22 | +import java.time.Instant; |
| 23 | +import java.util.Date; |
| 24 | + |
| 25 | +/** |
| 26 | + * A Java Program to get current timestamp using using the java.time.Instant |
| 27 | + * |
| 28 | + * @author Gaurav Kukade at coderolls.com |
| 29 | + * |
| 30 | + */ |
| 31 | +public class InstantExample { |
| 32 | + |
| 33 | + public static void main(String[] args) { |
| 34 | + |
| 35 | + Instant instant = Instant.now(); |
| 36 | + System.out.println("1. instant: "+ instant); // 2021-05-13T15:05:18.734Z |
| 37 | + |
| 38 | + |
| 39 | + //convert date object to instant |
| 40 | + Date date = new Date(); |
| 41 | + Instant instantFromDate = date.toInstant(); |
| 42 | + |
| 43 | + System.out.println("2. instantFromDate: " +instantFromDate); // 2021-05-13T15:05:18.873Z |
| 44 | + |
| 45 | + |
| 46 | + //get instnat from the java.sql.Timestamp |
| 47 | + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 48 | + Instant instantFromTimestamp = timestamp.toInstant(); |
| 49 | + |
| 50 | + System.out.println("3. instantFromTimestamp: "+ instantFromTimestamp); // 2021-05-13T15:05:18.874Z |
| 51 | + |
| 52 | + //get the epoch timestamp in milliseconds |
| 53 | + //epoch timestamp is number of milliseconds since January 1, 1970, 00:00:00 GMT |
| 54 | + Instant instant2 = Instant.now(); |
| 55 | + long timeStampMillis = instant2.toEpochMilli(); |
| 56 | + |
| 57 | + System.out.println("4. timeStampMillis: "+ timeStampMillis); // 1620918318874 |
| 58 | + |
| 59 | + //get the epoch timestamp in seconds |
| 60 | + Instant instant3 = Instant.now(); |
| 61 | + long timeStampSeconds = instant3.getEpochSecond(); |
| 62 | + |
| 63 | + System.out.println("5. timeStampSeconds: "+ timeStampSeconds); // 1620918318 |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +``` |
| 69 | +Output: |
| 70 | +``` |
| 71 | +1. instant: 2021-05-13T15:24:19.907Z |
| 72 | +2. instantFromDate: 2021-05-13T15:24:19.988Z |
| 73 | +3. instantFromTimestamp: 2021-05-13T15:24:19.988Z |
| 74 | +4. timeStampMillis: 1620919459988 |
| 75 | +5. timeStampSeconds: 1620919459 |
| 76 | +``` |
| 77 | +### Explanation: |
| 78 | + |
| 79 | +1. In the first case, we have used the `.now()` method of the `Instant` class to get the current timestamp. |
| 80 | +2. In the second case, we have created a new `date` object, and then we have used the `.toInstant()` on the `date` object to get the current timestamp. |
| 81 | +3. In the third case, we have created a new `timestamp` object using the system time in milliseconds. We can get the system time in millisecond using the `System.currentTimeMillis()`. To get the current timestamp (as instant) we have used the `.toInstant()` method on the `timestamp` object. |
| 82 | +4. In the fourth case, we are trying to get the epoch timestamp in milliseconds. We can get it using the `.toEpochMilli()` method on the `instant` object. |
| 83 | +5. In the fifth case, we are trying to get the epoch timestamp in seconds. We can get it using the `.getEpochSecond()` method on the `instant` object. |
| 84 | + |
| 85 | +## 2. Using `java.sql.Timestamp` ( for Java 7 and below) |
| 86 | + |
| 87 | +```java |
| 88 | +import java.sql.Timestamp; |
| 89 | +import java.util.Date; |
| 90 | + |
| 91 | +/** |
| 92 | + * A Java program to get the current timestamp using java.sql.Timestamp |
| 93 | + * |
| 94 | + * @author Gaurav Kukade at coderolls.com |
| 95 | + * |
| 96 | + */ |
| 97 | +public class TimestampExample { |
| 98 | + |
| 99 | + public static void main(String[] args) { |
| 100 | + |
| 101 | + //using system time in milliseconds |
| 102 | + Timestamp timestampFromSystemTime = new Timestamp(System.currentTimeMillis()); |
| 103 | + System.out.println("1. timestampFromSystemTime: "+timestampFromSystemTime); // 2021-05-13 20:55:44.179 |
| 104 | + |
| 105 | + //using java.util.Date |
| 106 | + Date date = new Date(); |
| 107 | + Timestamp timestampFromDateObject = new Timestamp(date.getTime()); |
| 108 | + System.out.println("2. timestampFromDateObject: " +timestampFromDateObject); // 2021-05-13 20:55:44.187 |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | +} |
| 113 | +``` |
| 114 | +Output: |
| 115 | + |
| 116 | +``` |
| 117 | +1. timestampFromSystemTime: 2021-05-13 20:55:44.179 |
| 118 | +2. timestampFromDateObject: 2021-05-13 20:55:44.187 |
| 119 | +``` |
| 120 | + |
| 121 | +### Explanation: |
| 122 | + |
| 123 | +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()`. |
| 124 | +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. |
| 125 | + |
| 126 | +Sometimes we may need to convert the `java.time.Instant` to `java.sql.Timestamp` or vice versa. So let's see both conversions below. |
| 127 | + |
| 128 | +## How to convert `Instant` to `Timestamp` In Java? |
| 129 | + |
| 130 | +I have given an example below that converts the `java.time.Instant` to `java.sql.Timestamp` class. |
| 131 | + |
| 132 | +```java |
| 133 | +import java.sql.Timestamp; |
| 134 | +import java.time.Instant; |
| 135 | + |
| 136 | +/** |
| 137 | + * A Java Program to convert java.time.Instant to java.sql.Timestamp |
| 138 | + * |
| 139 | + * @author Gaurav Kukade at coderolls.com |
| 140 | + * |
| 141 | + */ |
| 142 | +public class InstantToTimestamp { |
| 143 | + |
| 144 | + public static void main(String[] args) { |
| 145 | + |
| 146 | + // get Timestamp from Instant |
| 147 | + Instant instant = Instant.now(); |
| 148 | + Timestamp timestamp = Timestamp.from(instant); |
| 149 | + System.out.println("Instant To Timestamp: " +timestamp); // 2021-05-13 21:07:44.381 |
| 150 | + |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | +} |
| 155 | +``` |
| 156 | + |
| 157 | +Output: |
| 158 | +``` |
| 159 | +Instant To Timestamp: 2021-05-13 21:07:44.381 |
| 160 | +``` |
| 161 | +### Explanation: |
| 162 | + |
| 163 | +In the above example, we have used `.from()` of the `Timestamp` class to get the current timestamp from the `instant` object. |
| 164 | + |
| 165 | +```java |
| 166 | +Timestamp timestamp = Timestamp.from(instant); |
| 167 | +``` |
| 168 | + |
| 169 | + |
| 170 | +## How to convert `Timestamp` to `Instant` In Java? |
| 171 | + |
| 172 | +I have given an example below that converts the `java.sql.Timestamp` to `java.time.Instant` class. |
| 173 | + |
| 174 | +```java |
| 175 | +import java.sql.Timestamp; |
| 176 | +import java.time.Instant; |
| 177 | + |
| 178 | +/** |
| 179 | + * A Java Program to convert java.sql.Timestamp to java.time.Instant |
| 180 | + * |
| 181 | + * @author Gaurav Kukade at coderolls.com |
| 182 | + * |
| 183 | + */ |
| 184 | +public class TimestampToInstant { |
| 185 | + |
| 186 | + public static void main(String[] args) { |
| 187 | + |
| 188 | + //get instnat from the java.sql.Timestamp |
| 189 | + Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 190 | + Instant instant = timestamp.toInstant(); |
| 191 | + System.out.println("Timestamp To Instant: " +instant); // 2021-05-13T15:39:08.046Z |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +Output: |
| 199 | +``` |
| 200 | +Timestamp To Instant: 2021-05-13T15:39:08.046Z |
| 201 | +``` |
| 202 | +### Explanation: |
| 203 | + |
| 204 | +In the above example, we have used the `.toInstant()` method on the `timestamp` object to get the current timestamp (as Instant ). |
| 205 | + |
| 206 | +## Conclusion |
| 207 | + |
| 208 | +We can get the current timestamp in java using the `Instant` and `Timestamp`. |
| 209 | + |
| 210 | +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, |
| 211 | + |
| 212 | +```java |
| 213 | +Instant instant = Instant.now(); |
| 214 | + |
| 215 | +//convert date object to instant |
| 216 | +Date date = new Date(); |
| 217 | +Instant instantFromDate = date.toInstant(); |
| 218 | + |
| 219 | +//get instnat from the java.sql.Timestamp |
| 220 | +Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 221 | +Instant instantFromTimestamp = timestamp.toInstant(); |
| 222 | + |
| 223 | + |
| 224 | +//get the epoch timestamp in milliseconds |
| 225 | +//epoch timestamp is number of milliseconds since January 1, 1970, 00:00:00 GMT |
| 226 | +Instant instant2 = Instant.now(); |
| 227 | +long timeStampMillis = instant2.toEpochMilli(); |
| 228 | + |
| 229 | + |
| 230 | +//get the epoch timestamp in seconds |
| 231 | +Instant instant3 = Instant.now(); |
| 232 | +long timeStampSeconds = instant3.getEpochSecond(); |
| 233 | +``` |
| 234 | + |
| 235 | +In Java 7 or below, we can use the `Timestamp` class to get the current timestamp. |
| 236 | + |
| 237 | +```java |
| 238 | + //using system time in milliseconds |
| 239 | +Timestamp timestampFromSystemTime = new Timestamp(System.currentTimeMillis()); |
| 240 | + |
| 241 | +//using java.util.Date |
| 242 | +Date date = new Date(); |
| 243 | +System.out.println(date.getTime()); |
| 244 | +Timestamp timestampFromDateObject = new Timestamp(date.getTime()); |
| 245 | + |
| 246 | +``` |
| 247 | +Also we can convert the `java.time.Instant` to `java.sql.Timestamp` or vice versa. |
| 248 | + |
| 249 | + |
| 250 | +**Instant To Timestamp** |
| 251 | + |
| 252 | +```java |
| 253 | +Instant instant = Instant.now(); |
| 254 | +Timestamp timestamp = Timestamp.from(instant); |
| 255 | +``` |
| 256 | + |
| 257 | +**Timestamp to Instant** |
| 258 | + |
| 259 | +```java |
| 260 | +Timestamp timestamp = new Timestamp(System.currentTimeMillis()); |
| 261 | + Instant instant = timestamp.toInstant(); |
| 262 | +``` |
| 263 | + |
| 264 | +That's it for now. |
| 265 | + |
| 266 | +If you have any queries about the above article, please comment below. Thanks. |
| 267 | + |
| 268 | +We will see how to get the current date-time in java. Also, we will see how we can format them using the SimpleDateFormat. |
| 269 | + |
| 270 | +You can check my YouTube channel at: https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w |
| 271 | + |
| 272 | +### Related Articles |
| 273 | + |
| 274 | +- [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/) |
| 275 | + |
| 276 | +- [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/) |
| 277 | + |
| 278 | +- [How To Convert StringBuilder To String In Java?](https://coderolls.com/convert-stringbuilder-to-string-in-java/) |
| 279 | + |
| 280 | +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 281 | + |
| 282 | +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 283 | + |
0 commit comments