Write a Java Program to Get a date from milliseconds using the Date.setTime() method


This program demonstrates how to get and set the milliseconds value of a Date object. The program first creates a new Date object, which represents the current date and time. It then uses the getTime() method to get the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 GMT (also known as the "epoch").

The program then sets the number of milliseconds of the Date object to 732153600000L, which represents a specific date and time in milliseconds. This value corresponds to January 1, 1993, 00:00:00 GMT. Finally, the program prints out both the current date and time, as well as the date and time that was set using the specified number of milliseconds.

Source Code

import java.util.*;
public class Get_Milliseconds
{
	public static void main(String[] args)
	{
		Date date = new Date();
		System.out.println("Current DateTime : " + date);
		date.setTime(732153600000L);
		System.out.println("Datetime from Specified Milliseconds : " + date);
	}
}

Output

Current DateTime : Sat Dec 17 14:41:00 IST 2022
Datetime from Specified Milliseconds : Mon Mar 15 05:30:00 IST 1993

Example Programs