Home » Core Java » util » Date » Convert Date to millisec

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Convert Date to millisec

In this example we shall show you how to convert a Date to millisec, that means get the long number of millisec that represent a Date object. A Date object represents a specific instant in time, with millisecond precision. To convert a Date to millisec one should perform the following steps:

  • Create a new Date object, using the Date() constructor, that allocates a Date object and initializes it so that it represents the time at which it was allocated, measured to the nearest millisecond.
  • Use getTime() API method of Date. The method returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this Date object,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.Date;

public class DateToMilliSec{
 
  public static void main(String args[]){
 
  Date date = new Date();
  
  // Print date 
  System.out.println("Date = " + date);
 
  //getTime() returns the time that passed since 1970 00:00:00 GMT, in milliseconds 
  System.out.println("Milliseconds = " + date.getTime());
   
  }
 
}

Output:

Date = Sun Jul 22 18:58:19 EEST 2012
Milliseconds = 1342972699977

 
This was an example of how to convert a Date to millisec in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments