Calendar setTime() Method in Java with Examples Last Updated : 06 Mar, 2019 Comments Improve Suggest changes Like Article Like Report The setTime(Date dt) method in Calendar class is used to set Calendars time represented by this Calendar's time value, with the given or passed date as a parameter. Syntax: public final void setTime(Date dt)) Parameters: The method takes one parameter dt of Date type and refers to the given date that is to be set. Return Value: The method does not return any value. Below programs illustrate the working of setTime() Method of Calendar class: Example 1: Java // Java code to illustrate // setTime() method import java.util.*; public class Calendar_Demo extends GregorianCalendar { public static void main(String[] args) { // Creating calendar objects Calendar calndr1 = (Calendar)Calendar.getInstance(); Calendar calndr2 = (Calendar)Calendar.getInstance(); // Displaying the current date System.out.println("The Current" + " System Date: " + calndr1.getTime()); // Setting to a different date calndr1.set(Calendar.MONTH, 5); calndr1.set(Calendar.YEAR, 2006); calndr1.set(Calendar.DAY_OF_WEEK, 15); Date dt = calndr1.getTime(); // Setting the timevalue calndr2.setTime(dt); // Displaying the new date System.out.println("The modified" + " Date:" + calndr2.getTime()); } } Output: The Current System Date: Fri Feb 22 07:33:13 UTC 2019 The modified Date:Sun Jun 18 07:33:13 UTC 2006 Example 2: Java // Java code to illustrate // setTime() method import java.util.*; public class Calendar_Demo extends GregorianCalendar { public static void main(String[] args) { // Creating calendar objects Calendar calndr1 = (Calendar)Calendar.getInstance(); Calendar calndr2 = (Calendar)Calendar.getInstance(); // Displaying the current date System.out.println("The Current" + " System Date: " + calndr1.getTime()); // Setting to a different date calndr1.set(Calendar.MONTH, 10); calndr1.set(Calendar.YEAR, 1995); calndr1.set(Calendar.DAY_OF_WEEK, 20); Date dt = calndr1.getTime(); // Setting the timevalue calndr2.setTime(dt); // Displaying the new date System.out.println("The modified" + " Date: " + calndr2.getTime()); } } Output: The Current System Date: Fri Feb 22 07:33:20 UTC 2019 The modified Date: Fri Nov 24 07:33:20 UTC 1995 Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#setTime(java.util.Date) Comment More infoAdvertise with us Next Article Calendar setTime() Method in Java with Examples chinmoy lenka Follow Improve Article Tags : Misc Java Java - util package Java-Functions Java-Calendar +1 More Practice Tags : JavaMisc Similar Reads Calendar setTimeZone() Method in Java with Examples The setTimeZone(TimeZone time_zone) method in Calendar class takes a Time Zone value as a parameter and modifies or set the timezone represented by this Calendar. Syntax: public void setTimeZone(TimeZone time_zone) Parameters: The method takes one parameter time_zone of Date type and refers to the g 2 min read Calendar setTimeInMillis() Method in Java with Examples The setTimeInMillis(long mill_sec) method in Calendar class is used to set Calendars time represented by this Calendar from the passed long value. Syntax: public void setTimeInMillis(long mill_sec) Parameters: The method takes one parameter mill_sec of long datat-type and refers to the given date th 2 min read Date setTime() method in Java with Examples 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: 2 min read Calendar toString() Method in Java with Examples The toString() method in Calendar class is used to get the string representation of the Calendar object. This method in Calendar Class is just for debug process and not to be used as an operation. Syntax: public String toString() Parameters: The method does not take any parameters. Return Value: The 1 min read Calendar add() Method in Java with Examples The add() method of Calendar class present inside is used to add or subtract from the given calendar field(int field), a specific amount of time(int amt), based on the calendar's rules. Syntax: public abstract void add(int field, int amt) Parameters: The method takes two parameters: The field of the 3 min read Calendar get() method in Java with Examples The get(int field_value) method of Calendar class is used to return the value of the given calendar field in the parameter. Syntax: public int get(int field) Parameters: The method takes one parameter field_value of integer type and refers to the calendar whose value is needed to be returned. Return 2 min read Calendar clear() Method in Java with Examples The clear() method in Calendar class is used to set all the calendar field values and the time value of this Calendar undefined. Note: Implementation of calendar class may use default field values for date and time calculations. Syntax: public final void clear() Parameters: The method does not take 2 min read Calendar complete() Method in Java with Examples The complete() method in Calendar class is used to fill any unset fields of the calendar fields. It follows the following process of completion: At first, if the time value has not been calculated, the computeTime() method is called to calculate it from calendar field values. Second, to calculate al 2 min read util.date class methods in Java with Examples Following are some important date class methods : .toString() : java.util.Date.tostring() method is a java.util.Date class method.It displays the Current date and time. Here Date object is converted to a string and represented as: day mon dd hh:mm:ss zz yyyy day : day of the week mon : month dd : da 5 min read Calendar compareTo() Method in Java with Examples The add(Calendar Calendar2) method of Calendar class is used to compare the time values or the millisecond offsets of this Calendar object with the passed Calendar object. Syntax: public int compareTo(Calendar Calendar2) Parameters: The method takes one parameter Calendar2 of Calendar object type an 2 min read Like