Sunday 22 July 2018

How to use add(int field, int amount) method of Java.util.calendar class


Click here to watch on Youtube: 
https://www.youtube.com/watch?v=zkUsgRG68AQ&list=UUhwKlOVR041tngjerWxVccw

CalendarDemo.java

import java.util.Calendar;

public class CalendarDemo
{
    public static void main(String[] args)
    {
        Calendar cal = Calendar.getInstance();
        System.out.println("The current date is = " + cal.getTime());

        /*
         * Parameters:
         *
         * field - the calendar field.
         *
         * amount - the amount of date or time to be added to the field.
         */

       
        // add 20 days to the calendar
        cal.add(Calendar.DATE, 20);
        System.out.println("20 days later       = " + cal.getTime());

        // add 3 months to the calendar
        cal.add(Calendar.MONTH, 3);
        System.out.println("3 months later      = " + cal.getTime());

        // add 10 years to the calendar
        cal.add(Calendar.YEAR, 10);
        System.out.println("10 years later      = " + cal.getTime());
    }
}

Output

The current date is = Sat Jun 30 08:58:24 IST 2018
20 days later       = Fri Jul 20 08:58:24 IST 2018
3 months later      = Sat Oct 20 08:58:24 IST 2018
10 years later      = Fri Oct 20 08:58:24 IST 2028

Click the below link to download the code:
https://sites.google.com/site/ramj2eev2/java_basics/CalendarDemo_add.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava_2018/CalendarDemo_add

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/b1e669f4abac38a0eb65bfeb213dca88c58230cc/BasicJava_2018/CalendarDemo_add/?at=master

See also:
  • All JavaEE Videos Playlist
  • All JavaEE Videos
  • All JAVA EE Links
  • Spring Tutorial
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Cooking Tutorial
  • No comments:

    Post a Comment