Date to Calendar Java


Sometimes in Java, it is necessary to convert in between Date to Calendar and Calendar to Date classes.

Note: It is assumed that you know about the basic concepts of class Date and class GregorianCalendar.

It is done in the following code.

import java.util.*;
public class DateToCalendar
{
  public static void main(String args[])
  {
   Date d1 = new Date();                    // create java.util.Date object
   System.out.println(d1);
	
   Calendar cal1 = Calendar.getInstance();  // create Calendar object
   cal1.setTime(d1);                        // now Date comes into Calendar
   System.out.println(cal1);    
  }
}

Date to Calendar JavaOutput screen on Date to Calendar conversion Example

Create Date object and pass it to setTime() method of Calendar class. Calendar is abstract class. You cannot create object directly.

A. Following postings of this same Web site give all the possible operations on Date and Time manipulations.

1. class Date
2. class GregorianCalendar
3. SimpleDateFormat to set time as per the country with Locale
4. SimpleDateFormat to convert Date object to String form
5. SimpleDateFormat to convert String to Date
6. Java Dates Comparison

B. Conversions between java.util.Date, java.util.Calendar and java.sql.Date

1. Date Calendar Conversions

2 thoughts on “Date to Calendar Java”

Leave a Comment

Your email address will not be published.