The Wayback Machine - https://web.archive.org/web/20231108112536/https://www.geeksforgeeks.org/date-gettime-method-in-java-with-examples/
Open In App
Related Articles

Date getTime() method in Java with Examples

Improve Article
Improve
Save Article
Save
Like Article
Like

The getTime() method of Java Date class returns the number of milliseconds since January 1, 1970, 00:00:00 GTM which is represented by Date object.

Syntax:

public long getTime()

Parameters: The function does not accept any parameter.

Return Value: It returns the number of milliseconds since January 1, 1970, 00:00:00 GTM.

Exception: The function does not throws any exception.

Program below demonstrates the above mentioned function:




// Java code to demonstrate
// getTime() function of Date class
  
import java.util.Date;
import java.util.Calendar;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a Calendar object
        Calendar c1 = Calendar.getInstance();
  
        // set Month
        // MONTH starts with 0 i.e. ( 0 - Jan)
        c1.set(Calendar.MONTH, 11);
  
        // set Date
        c1.set(Calendar.DATE, 05);
  
        // set Year
        c1.set(Calendar.YEAR, 1996);
  
        // creating a date object with specified time.
        Date dateOne = c1.getTime();
  
        System.out.println("Date: " + dateOne);
  
        System.out.println(dateOne.getTime());
    }
}


Output:

Date: Thu Dec 05 09:29:39 UTC 1996
849778179420




// Java code to demonstrate
// getTime() function of Date class
  
import java.util.Date;
import java.util.Calendar;
public class GfG {
    // main method
    public static void main(String[] args)
    {
  
        // creating a Calendar object
        Calendar c1 = Calendar.getInstance();
  
        // set Month
        // MONTH starts with 0 i.e. ( 0 - Jan)
        c1.set(Calendar.MONTH, 11);
  
        // set Date
        c1.set(Calendar.DATE, 05);
  
        // set Year
        c1.set(Calendar.YEAR, 2000);
  
        // creating a date object with specified time.
        Date dateOne = c1.getTime();
  
        System.out.println("Date: " + dateOne);
  
        System.out.println(dateOne.getTime());
    }
}


Output:

Date: Tue Dec 05 09:29:40 UTC 2000
976008580370

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 02 Jan, 2019
Like Article
Save Article
Similar Reads
Related Tutorials