.getTimeInMillis()
Anonymous contributor
Published Aug 31, 2023
Contribute to Docs
The .getTimeInMillis()
method of the Calendar
class returns the time represented by a Calendar
object in the form of milliseconds.
Syntax
calendar.getTimeInMillis()
Returns the time represented by this Calendar
object in milliseconds as a long
.
Example
This example code creates a Calendar
object, records the current time, sets a new time, and then prints the difference.
import java.util.Calendar;// CalendarGetTimeInMillisExample.javapublic class CalendarGetTimeInMillisExample {public static void main(String[] args) {// Create a Calendar instanceCalendar calendar = Calendar.getInstance();// Get the current time in millisecondslong currentTime = calendar.getTimeInMillis();// Sets the time back to 12:00 AM todaycalendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);calendar.set(Calendar.MILLISECOND, 0);// Get the time in milliseconds of 12:00 AM todaylong earlierToday = calendar.getTimeInMillis();// Prints current amount of milliseconds that have passed todaySystem.out.println("Time passed today so far in milliseconds: " + (currentTime - earlierToday));}}
The output should be similar to:
Time passed today so far in milliseconds: 77753075
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.