
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Time Functions in Java
Java provides the Date class available in java.util package, this class encapsulates the current date and time. The time functions can be accessed from the java.util.Date class. This represents an instance of time with millisecond precision.
One of the time function in Java is the getTime() function. It returns the number of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. A program that demonstrates this is given as follows −
Example
import java.util.*; public class Example { public static void main(String[] args) { Date d = new Date(95, 7, 15); long num = d.getTime(); System.out.println("From the date 1-1-1970 to the date 15-07-1995, " + num + " milliseconds have passed."); } }
Output
From the date 1-1-1970 to the date 15-07-1995, 808444800000 milliseconds have passed.
Now let us understand the above program.
First, a date is created. Then the function getTime() is used to find the number of milliseconds that have passed since January 1, 1970, 00:00:00 GMT. Then this is displayed. The code snippet that demonstrates this is given as follows −
Date d = new Date(95, 7, 15); long num = d.getTime(); System.out.println("From the date 1-1-1970 to the date 15-07-1995, " + num + " milliseconds have passed.");
Advertisements