Table of Contents
In this post, we will get unix timestamp in Java.
As per wikipedia
Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, excluding leap seconds. The Unix epoch is 00:00:00 UTC on 1 January 1970.
Unix time is standard way to describe point in time and used at multiple unix like operating system.
Ways to get unix timestamp in Java
There are multiple ways to get unix timestamp in Java.
Using Instant class
You can use Instant
class to get unix timestamp in java in case you are using Java 8 or higher.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.time.Instant; public class GetUnixTimeStampMain { public static void main(String[] args) { long unixTimestamp = Instant.now().getEpochSecond(); System.out.println("Unix timestamp: "+unixTimestamp); } } |
Output
1 2 3 |
Unix timestamp: 1663659792 |
Using System.currentTimeMillis()
You can avoid date/Instant object creation and use System.currentTimeMillis()
to get current time in millisecond. You can convert milliseconds to seconds by diving it by 1000L
.
1 2 3 4 5 6 7 8 9 10 11 |
package org.arpit.java2blog; public class GetUnixTimeStampSystem { public static void main(String[] args) { long unixTimestamp = System.currentTimeMillis()/1000L; System.out.println("Unix timestamp: " + unixTimestamp); } } |
Output
1 2 3 |
Unix timestamp: 1663660088 |
Using Date’s getTime() method
You can use legacy Date’s getTime() method to get unix timestamp in Java. You need to divide() time by 1000L
to convert milliseconds to seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; import java.util.Date; public class GetUnixTimeStampDate { public static void main(String[] args) { long unixTimestamp = new Date().getTime()/1000L; System.out.println("Unix timestamp: " + unixTimestamp); } } |
Output
1 2 3 |
Unix timestamp: 1663660228 |
Convert Date to unix timestamp in Java
Using Instant class
You can get Instant()
from Date object using toInstant()
method and get unix timestamp using getEpochSecond()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.util.Calendar; import java.util.Date; public class GetUnixTimeStampDate { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.MONTH, 8); cal.set(Calendar.YEAR, 2022); Date givenDate = cal.getTime(); long unixTimestamp = givenDate.toInstant().getEpochSecond(); System.out.println("Unix timestamp: "+unixTimestamp); } } |
Output
1 2 3 |
Unix timestamp: 1663660884 |
You can also use LocalDate instead of java.util.Date
. You need to first convert LocalDate to Instant and use getEpochSecond()
to convert LocalDate to unix timestamp in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package org.arpit.java2blog; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneOffset; public class GetUnixTimeStampDate { public static void main(String[] args) { // Get LocalDate object LocalDate localDate = LocalDate.of(2022,10,23); // Convert LocalDate to Instant with ZoneOffSet Instant instant = localDate.atStartOfDay().toInstant(ZoneOffset.UTC); // Get unix timestamp from Instant long epochSecond = instant.getEpochSecond(); System.out.println("Unix timestamp: "+epochSecond); } } |
Output
1 2 3 |
Unix timestamp: 1666483200 |
Using Date’s getTime() method
You can use legacy Date’s getTime() method to convert Date to unixTimeStamp in Java. You need to divide() time by 1000L
to convert milliseconds to seconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog; import java.util.Calendar; import java.util.Date; public class GetUnixTimeStampDate { public static void main(String[] args) { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.MONTH, 8); cal.set(Calendar.YEAR, 2022); Date givenDate = cal.getTime(); long unixTimestamp = givenDate.getTime()/1000L; System.out.println("Unix timestamp: "+unixTimestamp); } } |
Output
1 2 3 |
Unix timestamp: 1663660228 |
That’s all about how to get Get Unix Timestamp in Java.