Home »
Java »
Java Programs
Java program to get milliseconds from the specified date
Learn how to get milliseconds from the specified date in Java?
Submitted by Nidhi, on March 29, 2022
Problem statement
In this program, we will get the milliseconds from a date in the specified format and print the result.
Source Code
The source code to get milliseconds from a specified date is given below. The given program is compiled and executed successfully.
// Java program to get milliseconds from
// specified date
import java.util.*;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-M-yyyy");
String dateString = "15-03-1993";
try {
Date dt = dateFormat.parse(dateString);
System.out.println("Date in milliseconds: " + dt.getTime());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output
Date in milliseconds: 732153600000
Explanation
In the above program, we created a class Main that contains a static method main(). The main() method is the entry point for the program, here we created a string variable that contains a date in a specified format. Then we parse the specified date and get the milliseconds using parse() and getTime() methods. After that, we printed the result.
Java Date and Time Programs »