Home  >  Core Java

Example of Calendar in java

By Arvind Rai, May 18, 2013
java.util.Calendar is an Abstract class. Calendar provides static getInstance method to get the object of Calendar. Calendar returns the time with default time zone and locale. Find different methods usability.
CalendarDemo.java
package com.util;
import java.util.Calendar;
public class CalendarDemo {
    public static void main(String[] args) {
        //creating Calendar object
        Calendar curCalendar = Calendar.getInstance();
        System.out.println(curCalendar.getTime());
        Calendar calendar = Calendar.getInstance();
        //updating current date by 5 month
        calendar.add(Calendar.DAY_OF_MONTH, 5);
        System.out.println(calendar.getTime());
        //using after
        System.out.println(calendar.after(curCalendar));
        //using before
        System.out.println(calendar.before(curCalendar));
        //0 means both time are equal, negative means time is before than argument time
        //and positive means time is greater than argument
        System.out.println(calendar.compareTo(curCalendar));
        //roll up the current date
        calendar.roll(Calendar.DATE, true);
        System.out.println(calendar.getTime());
        //roll down the current date
        calendar.roll(Calendar.DATE, false);
        System.out.println(calendar.getTime());
    }
}
Output
Fri May 17 11:42:30 IST 2013
Wed May 22 11:42:30 IST 2013
true
false
1
Thu May 23 11:42:30 IST 2013
Wed May 22 11:42:30 IST 2013 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us