Here you can find the source of toCalendar(Object value, String format)
Parameter | Description |
---|---|
value | the value to convert |
format | the DateFormat pattern to parse String values |
Parameter | Description |
---|---|
RuntimeException | thrown if the value cannot be converted to a Calendar |
public static Calendar toCalendar(Object value, String format)
//package com.java2s; //License from project: Open Source License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Main { /**/*from w w w . j a va2 s.co m*/ * Convert the specified object into a Calendar. * * @param value * the value to convert * @param format * the DateFormat pattern to parse String values * @return the converted value * @throws RuntimeException * thrown if the value cannot be converted to a Calendar */ public static Calendar toCalendar(Object value, String format) { if (value == null) return null; if (value instanceof Calendar) return (Calendar) value; if (value instanceof Date) { Calendar calendar = Calendar.getInstance(); calendar.setTime((Date) value); return calendar; } else if (value instanceof String) { try { Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat(format) .parse((String) value)); return calendar; } catch (ParseException e) { throw new RuntimeException("The value " + value + " can't be converted to a Calendar", e); } } throw new RuntimeException("The value " + value + " can't be converted to a Calendar"); } }