Date Manipulation in JAVA  


I had an requirement on date manipulation in Java and i had a very hard time searching on google. Finally i came to know on how to manipulate dates in Java and with this article i will be showing code snippets on the same. This code snippets is applicable in Core Java, JSP and Servlet Pages.

Formatting Dates:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Date newDate = new Date();
String newdate = dateformat.format(newDate);
System.out.println(newdate);

Adding Days to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);	//Adding 1 day to current date
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);


 


Adding Month to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 1);	//Adding 1 month to current date
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Adding Year to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, 1);	//Adding 1 year to current date
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);


 


Subtracting Year to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);	//-1 for subtracting 
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);


Subtracting Month to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1);	//-1 for subtracting 
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Subtracting Day to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);	//-1 for subtracting 
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Adding Hours to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, 1);	//Adding 1 hour to current date time
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Adding Minutes to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 10);	//Adding 10 minute to current date time
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Adding Seconds to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 40);	//Adding 40 minute to current date time
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Subtracting Hours to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);	//Subtracting 1 hour to current date time
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);

Subtracting Minutes to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, -10);	//Subtracting 10 minute to current date time
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);


Subtracting Seconds to Current Date:

SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, -40);	//Subtracting 40 minute to current date time
String newdate = dateformat.format(cal.getTime());
System.out.println(newdate);





Related Articles:

Categories: Java Tags:
  1. Revathi
    November 25th, 2008 at 22:24 | #1

    It’s really gud & useful , Thanks for giving a clear information abt Date function

    Thanks,
    Revathi

  2. Rohit
    December 10th, 2008 at 02:25 | #2

    Its really help me a lot
    Thax.

  3. jeeva
    December 17th, 2008 at 02:59 | #3

    gud one

  4. mint
    January 5th, 2009 at 18:10 | #4

    thanks. . this is a clear and good explanation

  5. Stian
    January 28th, 2009 at 05:23 | #5

    This really just goes to show how many code lines one has to write to do simple date manipulation in Java. There certainly is room for improvements in the Java language in this area.

    Good article.

  6. bynot
    February 21st, 2009 at 00:27 | #6

    how to implement Date manipulation without importing simple Date Format?? i need a code..pls reply asap.

  7. ISMAIL
    June 2nd, 2009 at 00:50 | #7

    thanks alot. It works nicely

  8. saran
    April 23rd, 2010 at 23:43 | #8

    thanks a lot.
    I need the java code of adding 5 days of current date excluding sundays. That means if the 5 days contains sunday it will skip that day and add next day

  9. Anup
    June 17th, 2010 at 06:12 | #9

    dis is wat i was lukin for….working perfectly fine :)
    very good article..!!

  10. rami
    January 7th, 2011 at 01:57 | #10

    eh my friends i have a proposition for manipulate a date in jsp/servlet you must simply to utilise the taglib fromat predefini in jsp/jstl search in sun.java it’s very simple and good chance

  11. anu
    June 5th, 2012 at 22:15 | #11

    hi,
    i would like to get the complete source in java for navigational calender with current date indicator.

  12. June 5th, 2012 at 22:17 | #12
  1. No trackbacks yet.

 

Page optimized by WP Minify WordPress Plugin