Write a Java Program to Get milliseconds from the specified date


The program takes a specific date in the format "dd-M-yyyy" as a string input and converts it into a Date object using SimpleDateFormat class. Then it prints the corresponding milliseconds of the date using getTime() method of Date class. Here's how the program works:

  • Import the necessary classes from java.util and java.text packages.
  • Define a Millisecond_SpecifiedDate class.
  • Inside the main method, create a SimpleDateFormat object named dt_formate with the format "dd-M-yyyy".
  • Create a string variable str_date and initialize it with "18-06-2014", which is a specific date in the format "dd-M-yyyy".
  • Use parse() method of SimpleDateFormat class to convert the string str_date into a Date object named dt.
  • Use getTime() method of Date class to get the milliseconds of the date and print it to the console.
  • Catch any exception that may occur during the execution of the program using a catch block.

Source Code

import java.util.*;
import java.text.SimpleDateFormat;
public class Millisecond_SpecifiedDate
{
	public static void main(String[] args)
	{
		SimpleDateFormat dt_formate = new SimpleDateFormat("dd-M-yyyy");
		String str_date = "18-06-2014";
		try
		{
		Date dt = dt_formate.parse(str_date);
		System.out.println("Date in Milliseconds : " + dt.getTime());
		}
		catch (Exception e)
		{
		e.printStackTrace();
		}
	}
}

Output

Date in Milliseconds : 1403029800000

Example Programs