Java Calendar Parse toCalendar(Object value, String format)

Here you can find the source of toCalendar(Object value, String format)

Description

Convert the specified object into a Calendar.

License

Open Source License

Parameter

Parameter Description
value the value to convert
format the DateFormat pattern to parse String values

Exception

Parameter Description
RuntimeException thrown if the value cannot be converted to a Calendar

Return

the converted value

Declaration

public static Calendar toCalendar(Object value, String format) 

Method Source Code

//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");
    }
}

Related

  1. str2Calendar(String str, String format)
  2. string2Calendar(String data)
  3. stringCalendar(Calendar cal)
  4. stringToCalendar(final String str, String format, boolean lenient)
  5. stringToCalendar(String fecha, String formato)
  6. toCalendar(String str)
  7. toGregorianCalendar(String value)
  8. toLocalCustomFormatCalendar( String calString, String format)