Date setTime() method in Java with Examples Last Updated : 07 Nov, 2019 Comments Improve Suggest changes 2 Likes Like Report 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: Java // Java code to demonstrate // setTime() function of Date class import java.util.Date; import java.util.Calendar; public class GfG { // main method public static void main(String[] args) { // creating a date object with specified time. Date dateOne = new Date(); System.out.println("Date initially: " + dateOne); // Sets the time dateOne.setTime(1000); // Prints the time 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: Java // Java code to demonstrate // setTime() function of Date class import java.util.Date; import java.util.Calendar; public class GfG { // main method public static void main(String[] args) { // creating a Calendar object Calendar c1 = Calendar.getInstance(); // set Month // MONTH starts with 0 i.e. ( 0 - Jan) c1.set(Calendar.MONTH, 11); // set Date c1.set(Calendar.DATE, 05); // set Year c1.set(Calendar.YEAR, 1996); // creating a date object with specified time. Date dateOne = c1.getTime(); System.out.println("Date initially: " + dateOne); // Sets the time dateOne.setTime(1000999); // Prints the time 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 Comment T Twinkl Bajaj Follow 2 Improve T Twinkl Bajaj Follow 2 Improve Article Tags : Misc Java Java - util package Java-Functions Java-util-Date +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface4 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like