Convert String to Calendar Object in Java

Convert String to Calendar Object in Java

Asked on December 14, 2018 in Java.
Add Comment


  • 5 Answer(s)

    Here is your solution,

    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
    cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
    

    Keep one thing in mind , Always set Locale according to your environment/requirement.

    Answered on December 14, 2018.
    Add Comment

    Using joda time, is better to designed date and time API. For example, you need to be aware that SimpleDateFormat is not thread-safe, so you either need thread-locals, synchronization, or a new instance each time you use it. Joda parsers and formatters are thread-safe.

    So only use the joda time instead of simpleDate format.

     

    Answered on December 14, 2018.
    Add Comment

    Try this solution,

    Actually, simpleDateFormat already uses a Calendar underneath.

    SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.EN_US);
    Date date = sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));// all done
    Calendar cal = sdf.getCalendar();
    
    Answered on December 14, 2018.
    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 18, 2019.
    Add Comment

    SimpleDateFormat is great, just note that HH is different from hh when working with hours. HH will return 24 hour based hours and hh will return 12 hour based hours.

    For example, the following will return 12 hour time:

    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm aa");

    While this will return 24 hour time:

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    Answered on January 18, 2019.
    Add Comment


  • Your Answer

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