How to Convert Date to Calendar Object in Java


In this example, we will write an example to introduce how to convert Date to Calendar object in Java.

Source Code

package com.beginner.examples;

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

public class ConvertDateToCalendar {

	public static void main(String[] args) {
		System.out.println(calendarToData(2019,4,9));
		Date date = new Date();
	    System.out.println(dataToCalendar(date).getTime());
	}
	public static Date calendarToData(int year, int month, int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(year, month - 1, day);
		Date date = calendar.getTime();
		return date;
	}
	 
	public static Calendar dataToCalendar(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar;
	}
}

Output:

Tue Apr 09 21:43:15 CST 2019
Tue Apr 09 21:43:15 CST 2019

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments