🎓 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
System.currentTimeMillis, Instant, LocalDateTime, ZonedDateTime, and Timestamp classes.Table of Contents
- Introduction
- Using
System.currentTimeMillis - Using
InstantClass - Using
LocalDateTimeClass - Using
ZonedDateTimeClass - Using
TimestampClass - Conclusion
Introduction
Java provides several classes and methods to work with dates and times. Depending on your requirements, such as time zone considerations and formatting needs, you can choose the most appropriate method.
Using System.currentTimeMillis
The System.currentTimeMillis method returns the current time in milliseconds since the Unix epoch (January 1, 1970, 00:00:00 GMT).
Example
public class TimestampExample {
public static void main(String[] args) {
long currentTimeMillis = System.currentTimeMillis();
System.out.println("Current timestamp in milliseconds: " + currentTimeMillis);
}
}
Explanation
System.currentTimeMillis(): Returns the current time in milliseconds since the Unix epoch.
Output:
Current timestamp in milliseconds: 1629955200000
Using Instant Class
The Instant class from the java.time package provides a machine-readable representation of a point in time.
Example
import java.time.Instant;
public class TimestampExample {
public static void main(String[] args) {
Instant currentTimestamp = Instant.now();
System.out.println("Current timestamp: " + currentTimestamp);
}
}
Explanation
Instant.now(): Returns the current timestamp as anInstantobject.
Output:
Current timestamp: 2023-08-25T14:30:00Z
Using LocalDateTime Class
The LocalDateTime class from the java.time package provides a date-time without a time-zone in the ISO-8601 calendar system.
Example
import java.time.LocalDateTime;
public class TimestampExample {
public static void main(String[] args) {
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current date and time: " + currentDateTime);
}
}
Explanation
LocalDateTime.now(): Returns the current date and time as aLocalDateTimeobject.
Output:
Current date and time: 2023-08-25T14:30:00
Using ZonedDateTime Class
The ZonedDateTime class from the java.time package provides a date-time with a time-zone in the ISO-8601 calendar system.
Example
import java.time.ZonedDateTime;
public class TimestampExample {
public static void main(String[] args) {
ZonedDateTime currentZonedDateTime = ZonedDateTime.now();
System.out.println("Current date and time with time zone: " + currentZonedDateTime);
}
}
Explanation
ZonedDateTime.now(): Returns the current date and time with time zone as aZonedDateTimeobject.
Output:
Current date and time with time zone: 2023-08-25T14:30:00+01:00[Europe/London]
Using Timestamp Class
The Timestamp class from the java.sql package is used for managing date-time values in SQL.
Example
import java.sql.Timestamp;
public class TimestampExample {
public static void main(String[] args) {
Timestamp currentTimestamp = new Timestamp(System.currentTimeMillis());
System.out.println("Current SQL timestamp: " + currentTimestamp);
}
}
Explanation
new Timestamp(System.currentTimeMillis()): Creates aTimestampobject using the current time in milliseconds.
Output:
Current SQL timestamp: 2023-08-25 14:30:00.0
Conclusion
Obtaining the current timestamp in Java can be accomplished using various methods, including the System.currentTimeMillis, Instant, LocalDateTime, ZonedDateTime, and Timestamp classes. Each method has its own advantages and specific use cases:
- The
System.currentTimeMillismethod is straightforward and provides the current time in milliseconds since the Unix epoch. - The
Instantclass provides a machine-readable representation of a point in time. - The
LocalDateTimeclass provides a date-time without a time zone. - The
ZonedDateTimeclass provides a date-time with a time zone. - The
Timestampclass is useful for managing date-time values in SQL.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with timestamps in Java.
Comments
Post a Comment
Leave Comment