How to modify date time (Date Manipulation) – Java

Java Calendar class (java.util.Calendar) is a very useful and handy class in java date time manipulation. here i will demonstrate how to modify date time with calender class.

Get current date time with Calendar()


DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));

Calender date time manipulation function


//Add one day to current date time
cal.add(Calendar.DATE, 1);

//Add one month to current date time
cal.add(Calendar.MONTH, 1);

//Add one year to current date time
cal.add(Calendar.YEAR, 1);

//Add one hour to current date time
cal.add(Calendar.HOUR, 1);

//Add one minute to current date time
cal.add(Calendar.MINUTE, 1);

//Add one second to current date time
cal.add(Calendar.SECOND, 1);

//Subtract one day from current date
cal.add(Calendar.DATE, -1);

//Subtract one month from current date
cal.add(Calendar.MONTH, -1);

//Subtract one year from current date
cal.add(Calendar.YEAR, -1);

//Subtract one hour from current date
cal.add(Calendar.HOUR, -1);

//Subtract one minute from current date
cal.add(Calendar.MINUTE, -1);

//Subtract one second from current date
cal.add(Calendar.SECOND, -1);

Here is the full source code to show how to modify date time in Java

 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
 
public class DateTimeManipulation {
  public static void main(String[] args) {
 
	   DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	   //get current date time with Calendar()
	   Calendar cal = Calendar.getInstance();
	   System.out.println("Current Date Time : " + dateFormat.format(cal.getTime()));
	   
	   cal.add(Calendar.DATE, 1);
	   System.out.println("Add one day to current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.MONTH, 1);
	   System.out.println("Add one month to current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.YEAR, 1);
	   System.out.println("Add one year to current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.HOUR, 1);
	   System.out.println("Add one hour to current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.MINUTE, 1);
	   System.out.println("Add one minute to current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.SECOND, 1);
	   System.out.println("Add one second to current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.DATE, -1);
	   System.out.println("Subtract one day from current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.MONTH, -1);
	   System.out.println("Subtract one month from current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.YEAR, -1);
	   System.out.println("Subtract one year from current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.HOUR, -1);
	   System.out.println("Subtract one hour from current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.MINUTE, -1);
	   System.out.println("Subtract one minute from current date : " + dateFormat.format(cal.getTime()));
	   
	   cal = Calendar.getInstance();
	   cal.add(Calendar.SECOND, -1);
	   System.out.println("Subtract one second from current date : " + dateFormat.format(cal.getTime()));	   
 
  }
}

Output

Current Date Time : 2008/12/28 10:24:53
Add one day to current date : 2008/12/29 10:24:53
Add one month to current date : 2009/01/28 10:24:53
Add one year to current date : 2009/12/28 10:24:53
Add one hour to current date : 2008/12/28 11:24:53
Add one minute to current date : 2008/12/28 10:25:53
Add one second to current date : 2008/12/28 10:24:54
Subtract one day from current date : 2008/12/27 10:24:53
Subtract one month from current date : 2008/11/28 10:24:53
Subtract one year from current date : 2007/12/28 10:24:53
Subtract one hour from current date : 2008/12/28 09:24:53
Subtract one minute from current date : 2008/12/28 10:23:53
Subtract one second from current date : 2008/12/28 10:24:52

10 comments on “How to modify date time (Date Manipulation) – Java

  1. I need program to set manual date and calculate time increment like as second, minute hours , date including leap year and output show in eclipse console

    Reply
  2. Trying to write a HTML Date Header, http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html I arrived this page …

    … and thanks to the combination of SimpleDateFormat (to which I assign the RFC “mandatory” timezone) and an instance of Calendar we can obtain an automatic translation of hour local time to absolute GMT time …

    public static String getHTMLDateHeader(){
    // http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html

    DateFormat dateFormat = new SimpleDateFormat(“EEE, dd MMM YYY HH:mm:ss z”, Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone(“GMT”));
    Calendar localDateTime = Calendar.getInstance();

    return dateFormat.format(localDateTime.getTime());
    }

    Thanks everybody!

    Reply
  3. I am a beginner and i work with datas . I have this kind of data “962409600000” and i have to change this data to datetime format and the result was “2000-07-01T05:45:00.000+05:45”.
    Please can you help me i need only the date 2000-07-01.

    Thanks

    Reply
  4. This information is very helpful.
    Thank you a million times

    Reply
  5. udhay. u may use as follows;

    z = 10;
    String dt = “2011-02-10”; // Start date
    SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);
    Calendar c = Calendar.getInstance();
    c.setTime(sdf.parse(dt));
    c.add(Calendar.DATE, z); // number of days to add
    dt = sdf.format(c.getTime());

    Reply
  6. Date cant be modified. But it could be used to compare dates by using getTimeInMillis().

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *