calendar date java code example

Example 1: convert date to calendar java

public static Calendar toCalendar(Date date){ 
  Calendar cal = Calendar.getInstance();
  cal.setTime(date);
  return cal;
}

Example 2: java Calendar add time

Calendar calendar = Calendar.getInstance();  
// 15 days ago
calendar.add(Calendar.DATE, -15); 
// After 4 months
calendar.add(Calendar.MONTH, 4);

Example 3: java Calender examokes

// Get curremt time and date.
Calendar calendar = Calendar.getInstance();  
System.out.println("The current date is : " + calendar.getTime());

Tags:

Java Example