🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
The concept of "Epoch Time" is frequently used in programming. It's especially common in scenarios where time and date need to be stored in a format that is easily sortable and comparable.
How to Get Epoch Time in Java 8
Java 8 introduced a new Date-Time API (java.time) that is comprehensive and more user-friendly, superseding the old date-time classes like java.util.Date and java.util.Calendar. We can now get the current epoch time in seconds or milliseconds using this API.import java.time.Instant;
public class Main {
public static void main(String[] args) {
// Get the current epoch time in seconds
long epochSeconds = Instant.now().getEpochSecond();
System.out.println("Epoch in Seconds: " + epochSeconds);
// Get the current epoch time in milliseconds
long epochMilliseconds = Instant.now().toEpochMilli();
System.out.println("Epoch in Milliseconds: " + epochMilliseconds);
}
}Output:
Epoch in Seconds: 1690263295
Epoch in Milliseconds: 1690263295605In the above example, Instant.now().getEpochSecond(); returns the current time in epoch seconds, and Instant.now().toEpochMilli(); returns the current time in epoch milliseconds.
The Instant class provides two methods to get the current time:
- getEpochSecond(): This method returns the current time in epoch seconds. It's equivalent to System.currentTimeMillis() / 1000L.
- toEpochMilli(): This method returns the current time in epoch milliseconds. It's equivalent to System.currentTimeMillis().
How to Get Epoch Time in Java (Older version)
If you are still using older versions of Java (Java 7 and older), you can get the epoch time using the java.util.Date class.
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Get the current epoch time in milliseconds
long epochMilliseconds = new Date().getTime();
System.out.println("Epoch in Milliseconds: " + epochMilliseconds);
}
}Output:
Epoch in Milliseconds: 1690263327269In this code snippet, new Date().getTime(); returns the current time in epoch milliseconds.Get the Current Timestamp in Java using System.currentTimeMillis()
import java.util.Date;
public class Main {
public static void main(String[] args) {
// Get epoch timestamp using System.currentTimeMillis()
long currentTimestamp = System.currentTimeMillis();
System.out.println("Current epoch timestamp in millis: " + currentTimestamp);
}
}Output:
Current epoch timestamp in millis: 1690263404149Conclusion
In this blog post, we discussed different ways to get the current epoch timestamp in seconds and milliseconds of precision using Java and Java 8 API.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
🆕 High-Demand
80–90% OFF
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
🆕 High-Demand
80–90% OFF
ChatGPT + Generative AI + Prompt Engineering for Beginners
🚀 Trending Now
80–90% OFF
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
🌟 Top Rated
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
🌟 Top Rated
80–90% OFF
Testing Spring Boot Application with JUnit and Mockito
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Master Spring Data JPA with Hibernate
🔥 Bestseller
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
🎓 Student Favorite
80–90% OFF
Available in Udemy for Business
Available in Udemy for Business
Comments
Post a Comment
Leave Comment