Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-25
Convert Unix Timestamp to Datetime in Java

How to convert Unix timestamp to Datetime in Java

🎈 🎈 🎈
+1

    import java.time.Instant;
    import java.time.LocalDateTime;
    import java.time.ZoneId;
    import java.time.format.DateTimeFormatter;
    
    public class UnixToDate {
        public static void main(String[] args) {
            long unixTimestamp = 1327581919L;
            LocalDateTime dateTime = Instant.ofEpochSecond(unixTimestamp)
                                            .atZone(ZoneId.systemDefault())
                                            .toLocalDateTime();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDate = dateTime.format(formatter);
            
            System.out.println("Converted Date: " + formattedDate);
        }
    }            
        

Output:
Converted Date: 2012-01-26 12:45:19
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Java code, the Unix timestamp (1327581919L) is first converted into an Instant object using the Instant.ofEpochSecond() method. This Instant represents the exact point in time corresponding to the Unix timestamp.
The Instant is then converted to a LocalDateTime object adjusted to the system's default time zone using atZone(ZoneId.systemDefault()).toLocalDateTime(). Finally, the LocalDateTime is formatted into a readable date and time string using DateTimeFormatter, in this case '2012-01-26 12:45:19'.


Other useful Java date functions

Function Description Example
new Date() Creates a new java.util.Date object representing the current date and time. new Date() returns Wed Aug 28 2024 12:34:56 GMT+0000 (Coordinated Universal Time)
Date.getTime() Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object. new Date().getTime() returns 1693227296000
Date.toString() Converts the Date object to a String in the default format. new Date().toString() returns Wed Aug 28 2024 12:34:56 GMT+0000 (Coordinated Universal Time)
LocalDate.now() Returns the current date from the system clock in the default time-zone. LocalDate.now() returns 2024-08-28
LocalDate.of(int year, int month, int dayOfMonth) Creates an instance of LocalDate with the specified year, month, and day. LocalDate.of(2024, 8, 28) returns 2024-08-28
LocalDate.parse(String text) Parses the given text to produce a LocalDate. LocalDate.parse("2024-08-28") returns 2024-08-28
LocalDateTime.now() Returns the current date and time from the system clock in the default time-zone. LocalDateTime.now() returns 2024-08-28T12:34:56.789
LocalDateTime.of(int year, int month, int day, int hour, int minute, int second) Creates an instance of LocalDateTime with the specified date and time. LocalDateTime.of(2024, 8, 28, 12, 34, 56) returns 2024-08-28T12:34:56
LocalDateTime.parse(String text) Parses the given text to produce a LocalDateTime. LocalDateTime.parse("2024-08-28T12:34:56") returns 2024-08-28T12:34:56
Instant.now() Returns the current instant from the system clock in UTC. Instant.now() returns 2024-08-28T12:34:56.789Z
Instant.ofEpochSecond(long epochSecond) Creates an Instant representing a specific point in time, given in seconds from the Unix epoch. Instant.ofEpochSecond(1693227296L) returns 2024-08-28T12:34:56Z
ZonedDateTime.now() Returns the current date and time with the time zone information. ZonedDateTime.now() returns 2024-08-28T12:34:56+02:00[Europe/Paris]
ZonedDateTime.of(LocalDateTime dateTime, ZoneId zone) Creates a ZonedDateTime from a LocalDateTime and a time zone. ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("Europe/Paris")) returns 2024-08-28T12:34:56+02:00[Europe/Paris]
DateTimeFormatter.ofPattern(String pattern) Creates a formatter using the specified pattern to format or parse dates and times. DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()) returns 2024-08-28 12:34:56
DateTimeFormatter.ISO_DATE_TIME Predefined formatter for ISO date-time format (e.g., "2024-08-28T12:34:56"). DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now()) returns 2024-08-28T12:34:56
ChronoUnit.DAYS.between(Temporal startInclusive, Temporal endExclusive) Calculates the number of days between two temporal objects. ChronoUnit.DAYS.between(LocalDate.of(2024, 1, 1), LocalDate.now()) returns 240 (example)
ZoneId.systemDefault() Retrieves the system default time zone. ZoneId.systemDefault() returns America/New_York
ZoneId.of(String zoneId) Obtains an instance of ZoneId from a string identifier. ZoneId.of("Europe/Paris") returns Europe/Paris
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-04-25
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder