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 Solution:

In this program, we will get the milliseconds from a date in the specified format and print the result.

Program/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 »



Related Programs

ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.