Write a Java program to get seconds since 1970


This program calculates the number of seconds that have elapsed since January 1, 1970, 00:00:00 GMT (also known as the Unix epoch).

  • The System.currentTimeMillis() method returns the current time in milliseconds since the epoch, and by dividing it by 1000, we get the time in seconds.
  • The l suffix after 1000 is added to indicate that the value should be treated as a long, to avoid any potential overflow errors when dividing.
  • The program then prints the number of seconds since the epoch using the println() method.

Source Code

public class Get_Seconds
{
	public static void main(String[] args)
	{
		long sec = System.currentTimeMillis() / 1000l;
		System.out.println("Seconds since 1970 = "+sec);
	}
}

Output

Seconds since 1970 = 1667633186

Example Programs