Date setTime() method in Java with Examples
Last Updated :
07 Nov, 2019
The setTime() method of Java Date class sets a date object. It sets date object to represent time milliseconds after January 1, 1970 00:00:00 GMT.
Syntax:
public void setTime(long time)
Parameters: The function accepts a single parameter time which specifies the number of milliseconds.
Return Value: It method has no return value.
Exception: The function does not throws any exception.
Program below demonstrates the above mentioned function:
Program 1:
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String[] args)
{
Date dateOne = new Date();
System.out.println( "Date initially: "
+ dateOne);
dateOne.setTime( 1000 );
System.out.println( "Date after setting"
+ " the time: "
+ dateOne);
}
}
|
Output:
Date initially: Wed Jan 02 09:34:03 UTC 2019
Date after setting the time: Thu Jan 01 00:00:01 UTC 1970
Program 2:
import java.util.Date;
import java.util.Calendar;
public class GfG {
public static void main(String[] args)
{
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.MONTH, 11 );
c1.set(Calendar.DATE, 05 );
c1.set(Calendar.YEAR, 1996 );
Date dateOne = c1.getTime();
System.out.println( "Date initially: "
+ dateOne);
dateOne.setTime( 1000999 );
System.out.println( "Date after setting"
+ " the time: "
+ dateOne);
}
}
|
Output:
Date initially: Thu Dec 05 09:32:53 UTC 1996
Date after setting the time: Thu Jan 01 00:16:40 UTC 1970
Share your thoughts in the comments
Please Login to comment...