Java: Convert Date Class Object to Calendar


import java.util.Calendar;
import java.util.Date;

public class GetDateTimeZoned {

    public static void main(String[] args) {

        Date currentDate = new Date();
        System.out.println(currentDate);
        Calendar calendar = convertDateToCalendar(currentDate);
        System.out.println(calendar);

    }

    public static Calendar convertDateToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }
}

You can convert java.util.Date class object to java.util.Calendar in three easy steps,

  1. Crete an instance of Date object.
  2. Create an instance of Calendar object.
  3. Now make use of the setTime(Date date) method on the Calendar object to convert the date into the calendar.

One should try and avoid using Date and Calendar classes from java.util package and move the Date-Time API introduced in Java 8.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap