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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.