Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Convert Datetime to Unix Timestamp in Java

How to convert Datetime to Unix timestamp in Java

🎈 🎈 🎈
+1

    import java.time.LocalDateTime;
    import java.time.ZoneOffset;
    
    public class DateToUnixTimestamp {
        public static void main(String[] args) {
            // Create a LocalDateTime object for a specific date and time
            LocalDateTime dateTime = LocalDateTime.of(2013, 10, 28, 07, 11, 29);
    
            // Convert the LocalDateTime to Unix timestamp (seconds since the epoch)
            long unixTimestamp = dateTime.toEpochSecond(ZoneOffset.UTC);
    
            // Print the Unix timestamp
            System.out.println("Unix Timestamp: " + unixTimestamp);
        }
    }            
        

Output:
Unix Timestamp: 1382944289
Example only. There may be multiple ways to perform this operation.

Code Explanation

In this Java snippet, the LocalDateTime.of() method is used to create a LocalDateTime object representing the specific date and time (2013-10-28 07:11:29).
The toEpochSecond() method is then called on this LocalDateTime object, with ZoneOffset.UTC passed as an argument to ensure that the conversion is based on UTC time.


Other useful Java date functions

Function Description Example
LocalDate.now() Returns the current date in the format 'YYYY-MM-DD'. LocalDate.now() returns 2024-08-28
LocalTime.now() Returns the current time in the format 'HH:mm:ss'. LocalTime.now() returns 12:34:56
LocalDateTime.now() Returns the current date and time in the format 'YYYY-MM-DDTHH:mm:ss'. LocalDateTime.now() returns 2024-08-28T12:34:56
Instant.now() Returns the current timestamp in UTC. Instant.now() returns 2024-08-28T12:34:56Z
LocalDate.parse() Parses a string to create a LocalDate object. LocalDate.parse("2024-08-28") returns 2024-08-28
LocalDateTime.parse() Parses a string to create a LocalDateTime object. LocalDateTime.parse("2024-08-28T12:34:56") returns 2024-08-28T12:34:56
LocalDateTime.of() Creates a LocalDateTime from specified date and time values. LocalDateTime.of(2024, 8, 28, 12, 34, 56) returns 2024-08-28T12:34:56
Instant.ofEpochMilli() Converts milliseconds since Unix epoch to an Instant. Instant.ofEpochMilli(1693227296789L) returns 2024-08-28T12:34:56.789Z
Instant.ofEpochSecond() Converts seconds since Unix epoch to an Instant. Instant.ofEpochSecond(1693227296L) returns 2024-08-28T12:34:56Z
LocalDateTime.toEpochSecond() Converts a LocalDateTime to Unix timestamp (seconds since epoch) with specified time zone. LocalDateTime.now().toEpochSecond(ZoneOffset.UTC) returns 1693227296
LocalDateTime.format() Formats the LocalDateTime using a DateTimeFormatter. LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) returns 2024-08-28 12:34:56
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]
Current Unix Timestamp SECONDS SINCE JAN 01 1970

Current UTC Time
2025-08-20
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder
Ad Banner Placeholder