🎓 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
In Java, dealing with different types of date and time objects is a common task. One such requirement is converting a Timestamp object to a LocalDateTime object. The Timestamp class, part of the java.sql package, represents a moment in time, typically used for timestamping in databases.
LocalDateTime, part of the Java 8 Date and Time API, represents a date-time without a time zone in the ISO-8601 calendar system. In this post, we'll explore how to convert a Timestamp to a LocalDateTime.
Understanding Timestamp and LocalDateTime
Timestamp: Represents a point in time, with precision up to nanoseconds, commonly used with SQL databases.
LocalDateTime: This does not contain information about the time zone or offset from UTC/Greenwich, making it a pure date-time representation.
Method 1: Using Timestamp.toLocalDateTime()
Java 8 introduced convenient methods to convert between old and new date-time classes. Timestamp.toLocalDateTime() is the most straightforward way to convert a Timestamp to a LocalDateTime.
import java.sql.Timestamp;
import java.time.LocalDateTime;
public class TimestampToLocalDateTime {
public static void main(String[] args) {
// Creating a Timestamp object
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// Converting Timestamp to LocalDateTime
LocalDateTime localDateTime = timestamp.toLocalDateTime();
// Display the result
System.out.println("LocalDateTime: " + localDateTime);
}
}
Output:
LocalDateTime: [Current local date-time]
Explanation:
- We create a Timestamp object representing the current moment.
- We use the toLocalDateTime() method of the Timestamp class to convert it into a LocalDateTime object.
- The converted LocalDateTime object is displayed, showing the date and time without timezone information.
Method 2: Using Timestamp.toInstant().atZone(ZoneId).toLocalDateTime()
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class TimestampConversion {
public static void main(String[] args) {
// Create a Timestamp object
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
// Convert Timestamp to LocalDateTime considering the system's default time zone
LocalDateTime localDateTime = timestamp.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// Display the result
System.out.println("LocalDateTime with Time Zone: " + localDateTime);
}
}
Output:
LocalDateTime with Time Zone: [Current date-time in the system's default format]
Conclusion
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