Write a Java program to get seconds since Specific Year

0 0
Read Time:41 Second

Write a Java program to get seconds since Specific Year

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.
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

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %