
- Epoch time, also known as Unix time or POSIX time, is a system for representing timestamps as the number of seconds that have elapsed since a specific reference point called the “epoch.”
- The epoch is a fixed point in time from which all other timestamps are measured.
- In the context of Unix-based operating systems and programming, the epoch time is defined as the number of seconds that have passed since January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). This moment is commonly referred to as the “Unix Epoch” or “Unix Time.”
- The use of epoch time has several advantages, including:
- Simplicity: Representing time as a single integer (number of seconds) simplifies date and time calculations in programming.
- Portability: Epoch time is not dependent on any time zone, making it easy to store and transmit timestamps across different systems and locations.
- Precision: Using seconds as the unit of measurement provides sufficient precision for many applications.
- Simplicity: Representing time as a single integer (number of seconds) simplifies date and time calculations in programming.
- To convert a specific timestamp into epoch time, you can calculate the number of seconds that have passed between that timestamp and the Unix Epoch. Conversely, to convert an epoch time back to a human-readable date and time, you can add the epoch time to the Unix Epoch reference point.
- Code to get epoch time and covert it to Local date and time in Java
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
public class EpochTime {
public static void main(String[] args) {
//Get epoch time
long epochSeconds = Instant.now().toEpochMilli();
System.out.println("Epoch in Milliseconds: " + epochSeconds);
// Convert epoch time to Java LocalDateTime
Instant instant = Instant.ofEpochMilli(epochSeconds);
LocalDateTime dateTime = instant.atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("Java LocalDateTime: " + dateTime);
}
}
Thanks !!