Converting a Date object to a calendar object

Converting a Date object to a calendar object

Asked on December 13, 2018 in Java.
Add Comment


  • 6 Answer(s)

    You can achieve this by simply write the code below,

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

    The Java Naming conventions something callled BTW, suggest that method names start with a lower case letter, so it should be: dateToCalendar or toCalendar.

    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    date = (Date)formatter.parse(date.toString());
    

    The convertion of Strings to Dates (parse()) or Dates to Strings (format()) is done by DateFormat  . You are using it to parse the String representation of a Date back to a Date.

    Answered on December 13, 2018.
    Add Comment

    You can use Apache Commons to get solution,

    DateUtils.toCalendar(Date date)

    Answered on December 13, 2018.
    Add Comment

    You can try this great code,

    Calendar cal=Calendar.getInstance();
    DateFormat format=new SimpleDateFormat("yyyy/mm/dd");
    format.format(date);
    cal=format.getCalendar();
    
    Answered on December 13, 2018.
    Add Comment

    In Java, you can use calendar.setTime(date) to convert a Date object to a Calendar object.

    	Calendar calendar = Calendar.getInstance();
    	Date newDate = calendar.setTime(date);
    Answered on January 17, 2019.
    Add Comment
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class DateAndCalendar {
    	public static void main(String[] argv) throws ParseException {
    
    		//1. Create a Date from String
    		SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    		String dateInString = "22-01-2015 10:20:56";
    		Date date = sdf.parse(dateInString);
                    DateAndCalendar obj = new DateAndCalendar();
    
    		//2. Test - Convert Date to Calendar
    		Calendar calendar = obj.dateToCalendar(date);
    		System.out.println(calendar.getTime());
    		
    		//3. Test - Convert Calendar to Date
    		Date newDate = obj.calendarToDate(calendar);
    		System.out.println(newDate);
    
    	}
    
    	//Convert Date to Calendar
    	private Calendar dateToCalendar(Date date) {
    
    		Calendar calendar = Calendar.getInstance();
    		calendar.setTime(date);
    		return calendar;
    
    	}
    
    	//Convert Calendar to Date
    	private Date calendarToDate(Calendar calendar) {
    		return calendar.getTime();
    	}
    
    }
    Answered on January 17, 2019.
    Add Comment

    Java, you can use calendar.setTime(date) to convert a Date object to a Calendar object.

    1
    2
    3
    4
    5
    public static Calendar toCalendar(Date date){
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      return cal;
    }

    The Java Naming conventions something callled BTW, suggest that method names start with a lower case letter, so it should be: dateToCalendar or toCalendar.

    1
    2
    DateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    date = (Date)formatter.parse(date.toString());
    Answered on January 18, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.