Java Utility Library

Java Date - setTime() Method



The java.util.Date.setTime() method is used to set this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

Syntax

public void setTime(long time)

Parameters

time Specify the number of milliseconds.

Return Value

void type.

Exception

NA

Example:

In the example below, the java.util.Date.setTime() method is used to set the given Date object.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a Date
    Date Dt = new Date();

    //printing the Date
    System.out.println("Date before setting: " + Dt);

    //set the Date object for 3600000 milliseconds
    //after January 1, 1970 00:00:00 GMT
    Dt.setTime(3600000);

    //printing the Date
    System.out.println("Date after setting: " + Dt);
  }
}

The output of the above code will be:

Date before setting: Sat May 08 11:03:56 UTC 2021
Date after setting: Thu Jan 01 01:00:00 UTC 1970

❮ Java.util - Date