java.util.Calendar setTimeInMillis(long millis)

Description :

This java tutorial shows how to use the setTimeInMillis(long millis) method of Calendar class of java.util package. This method sets this Calendar’s current time from the given long value.

Method Syntax :

public void setTimeInMillis(long millis)

Parameter Input :

 

DataType Parameter Description
long millis The value in milliseconds to be set as new time on the Calendar object.

 

Method Returns :

This method returns void

Compatibility Version :

Requires Java 1.1 and up

Exception :

N/A

Discussion :

The Calender setTimeInMillis(long millis) method is used to get the current time on our system in milliseconds. Personally i prefer to store in database or file a date in milliseconds rather than the full date. It is easier and does not require complicated handling of dates.

Java Code Example :

This java example source code demonstrates the use of setTimeInMillis(long millis) method of Calendar class.

package com.javatutorialhq.java.examples;

import java.util.Calendar; import java.util.Date;

/* * This example source code demonstrates the use of * setTimeInMillis(long millis) method of Calendar class */

public class CalendarSetTimeInMillisExample {

public static void main(String

[] args) throws InterruptedException {

Calendar cal = Calendar.getInstance(); // Printing the current time System.out.println("Current time:"+cal.getTime());

// set a new Date cal.setTimeInMillis(1421994054519L);

// Printing the new time System.out.println("New Time:"+cal.getTime());

}

}

Sample Output :

Running the setTimeInMillis(long millis) method example source code of Calendar class will give you the following output:

setTimeInMillis(long millis) method example

setTimeInMillis(long millis) method example

Exception Scenario :

N/A

Similar Method :

  • N/A

Suggested Reading List :

References :