Usage of Calendar Class and Manipulation.

Question 4:

Usage of Calendar Class and Manipulation.
For the students who find this question very simple can look at difference between two dates in the extra question section

Code:

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Test {

    public static void main(String[] args) {

        Calendar today = new GregorianCalendar();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        System.out.println("The date is " + dateFormat.format(today.getTime())+".");
        today.set(Calendar.DAY_OF_MONTH, today.get(Calendar.DAY_OF_MONTH)+30);
        System.out.println("Adding 30 days, The date is " + dateFormat.format(today.getTime())+".");
    }

}

Output:

The date is 12/12/2014.
Adding 30 days, The date is 11/01/2015.

No comments:

Post a Comment