Java program to convert Calendar to Date and Date to Calendar in java

How to convert Calendar to Date and Date to Calendar:

In this tutorial we covered how to use swapping java calendar and date , The following Java program is to convert from Calendar object to Date object and Date object to Calendar object.

Calendar object to Date object,

Using Calendar.getInstance() method the date from calendar object is obtained and using getTime() method it is converted to Date object.

Date object to Calendar object,

Date d=new Date(1515660075000l);

The above code is used to get date and time from date object. using getInstance() and setTime() method it is converted to Calendar object. The date is obtained using cal.getTime() method.

package com.candidjava.time;

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

public class CalendarToDate
	public static void main(String[] args) 
		//Calendar Object to Date Object
		 Calendar c=Calendar.getInstance();		
		 Date d1=c.getTime();
		//Date Object to Calendar Object
		Date d=new Date(15156600750001);
		Calendar cal=Calendar.getInstance();


Mon Jan 07 13:18:32 IST 2019
Mon Jan 07 13:18:32 IST 2019

Leave a Reply

Your email address will not be published. Required fields are marked *