🎓 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
Introduction
Epoch time, also known as Unix time, is the number of milliseconds that have elapsed since January 1, 1970 (midnight UTC). In Java 8, the new java.time package provides classes like LocalDate and LocalDateTime to handle date and time in a more flexible way. Converting epoch milliseconds to LocalDate or LocalDateTime is a common requirement when working with time-based data.
In this guide, we will learn how to convert epoch time in milliseconds to LocalDate or LocalDateTime using Java 8's Instant, ZoneId, and LocalDateTime.
Solution Steps
- Create
Instantfrom Epoch Milliseconds: Use theInstant.ofEpochMilli()method to convert epoch time in milliseconds to anInstant. - Convert to
LocalDateTime: UseLocalDateTime.ofInstant()to convert theInstantto aLocalDateTime. - Convert to
LocalDate: Extract theLocalDatefrom theLocalDateTimeor convert directly usingLocalDate.
Java Program
Method 1: Convert Epoch Time to LocalDateTime
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class EpochToLocalDateTime {
public static void main(String[] args) {
// Step 1: Define epoch time in milliseconds (Example: 1625812800000L is 2021-07-09 00:00:00 UTC)
long epochMilli = 1625812800000L;
// Step 2: Convert epoch milliseconds to LocalDateTime
LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneId.systemDefault());
// Step 3: Display the LocalDateTime
System.out.println("LocalDateTime: " + dateTime);
}
}
Output
LocalDateTime: 2021-07-09T05:30
Explanation
- Step 1: We define epoch time in milliseconds (
1625812800000L), which represents the date2021-07-09in UTC. - Step 2: We use
Instant.ofEpochMilli(epochMilli)to create anInstantfrom the epoch time, and then convert it toLocalDateTimeusingLocalDateTime.ofInstant(). TheZoneId.systemDefault()specifies the time zone of the system. - Step 3: The
LocalDateTimeis printed, which shows the date and time in the system's default time zone.
Method 2: Convert Epoch Time to LocalDate
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
public class EpochToLocalDate {
public static void main(String[] args) {
// Step 1: Define epoch time in milliseconds
long epochMilli = 1625812800000L;
// Step 2: Convert epoch milliseconds to LocalDate
LocalDate date = Instant.ofEpochMilli(epochMilli)
.atZone(ZoneId.systemDefault())
.toLocalDate();
// Step 3: Display the LocalDate
System.out.println("LocalDate: " + date);
}
}
Output
LocalDate: 2021-07-09
Explanation
- Step 1: We define the same epoch time in milliseconds.
- Step 2: We first convert the epoch time to an
InstantusingInstant.ofEpochMilli(), then convert it to aZonedDateTimeusing.atZone(ZoneId.systemDefault()), and finally extract theLocalDateusing.toLocalDate(). - Step 3: The result is printed as a
LocalDate, which shows only the date part.
Conclusion
Converting epoch time in milliseconds to LocalDate or LocalDateTime is simple with Java 8's Instant, LocalDateTime, and LocalDate classes. The key step is to use Instant.ofEpochMilli() to create an Instant and then convert it to the desired date or time format using LocalDateTime.ofInstant() or toLocalDate(). This approach ensures that the conversion takes into account the system's default time zone or any other time zone you may specify.
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
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment