Home »
Java programming language
Java Date setTime() Method with Example
Date Class setTime() method: Here, we are going to learn about the setTime() method of Date Class with its syntax and example.
Submitted by Preeti Jain, on February 09, 2020
Date Class setTime() method
- setTime() method is available in java.util package.
- setTime() method is used to set this date-time in milliseconds.
- setTime() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
- setTime() Method does not throw an exception at the time of set date time in milliseconds.
Syntax:
public void setTime(long t);
Parameter(s):
- long t – represents the time in milliseconds.
Return value:
The return type of this method is void, it returns nothing.
Example:
// Java program to demonstrate the example
// of void setTime(long t) method of Date
import java.util.*;
public class SetTimeOfDate {
public static void main(String[] args) {
// create a Date object with dates
Date this_date = new Date(2016, 8, 20);
// By using setTime() method is to set
//the time in milliseconds after 01'01'1970
this_date.setTime(15000);
//Display Date
System.out.println("this_date.hashCode(): " + this_date.toString());
}
}
Output
this_date.hashCode(): Thu Jan 01 00:00:15 GMT 1970