1. Overview
In this tutorial, We'll learn how to get the current timestamp in java in various ways.
Let us jump into the right examples with JDK 8 and older jdk.
2. Java Get Current Timestamp using TimeStamp class
Example 1:
package com.javaprogramto.java8.dates.timestamp;
import java.sql.Timestamp;
public class CurrentTimestampExample1 {
public static void main(String[] args) {
long timestampInMillies = System.currentTimeMillis();
Timestamp timestamp = new Timestamp(timestampInMillies);
System.out.println("timestamp now : "+timestamp);
}
}
Output:
timestamp now : 2021-11-22 19:57:00.7
3. Java Get Current Timestamp using Date.toInstant() Method
Example 2:
package com.javaprogramto.java8.dates.timestamp;
import java.time.Instant;
import java.util.Date;
public class CurrentTimestampExample2 {
public static void main(String[] args) {
Date dateNow = new Date();
Instant instant = dateNow.toInstant();
System.out.println("timestamp now : " + instant);
}
}
Output:
timestamp now : 2021-11-22T14:32:05.069Z
4. Java Get Current Timestamp using SimpleDateFormat and Date
Example 3:
package com.javaprogramto.java8.dates.timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
public class CurrentTimestampExample2 {
public static void main(String[] args) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateNow = new Date();
String timestampNow = simpleDateFormat.format(dateNow);
System.out.println("timestamp now : " + timestampNow);
}
}
Output:
timestamp now : 2021-11-22 20:04:49
5. Java Get Current Timestamp using TimeStamp to Instant
Example 4:
package com.javaprogramto.java8.dates.timestamp;
import java.sql.Timestamp;
import java.time.Instant;
public class CurrentTimestampExample4 {
public static void main(String[] args) {
long timeInMillis = System.currentTimeMillis();
Timestamp timestamp = new Timestamp(timeInMillis);
Instant instant = timestamp.toInstant();
System.out.println("timestamp now : " + instant);
}
}
Output:
timestamp now : 2021-11-22T14:38:22.927Z
6. Java Get Current Timestamp using Instant
Example 5:
package com.javaprogramto.java8.dates.timestamp;
import java.time.Instant;
public class CurrentTimestampExample5 {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println("timestamp now : " + instant);
}
}
Output:
timestamp now : 2021-11-22T14:38:22.927Z
7. Java Get Current Timestamp using LocalDateTime
Example 6:
package com.javaprogramto.java8.dates.timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentTimestampExample7 {
public static void main(String[] args) {
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
String timestamp = formatter.format(localDateTime);
System.out.println("timestamp now : " + timestamp);
}
}
Output:
timestamp now : 2021-11-22 20:14:06 725
8. Java Get Current Timestamp using ZonedDateTime
Example 7:
package com.javaprogramto.java8.dates.timestamp;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
public class CurrentTimestampExample6 {
public static void main(String[] args) {
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime localDateTime = ZonedDateTime.now(zoneId);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss SSS");
String timestamp = formatter.format(localDateTime);
System.out.println("timestamp now : " + timestamp);
}
}
Output:
timestamp now : 2021-11-22 20:16:18 416
9. Conclusion
In this article, we've seen the different 7 ways to get the timestamp in java.

No comments:
Post a Comment
Please do not add any spam links in the comments section.