Java Code Examples for java.text.DateFormat.getCalendar()

The following are Jave code examples for showing how to use getCalendar() of the java.text.DateFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: NotifyTools   File: DateTimeConverter.java   Source Code and License Vote up 6 votes
/**
 * Parse a String into a <code>Calendar</code> object
 * using the specified <code>DateFormat</code>.
 *
 * @param sourceType The type of the value being converted
 * @param targetType The type to convert the value to
 * @param value The String date value.
 * @param format The DateFormat to parse the String value.
 *
 * @return The converted Calendar object.
 * @throws ConversionException if the String cannot be converted.
 */
private Calendar parse(final Class<?> sourceType, final Class<?> targetType, final String value, final DateFormat format) {
    logFormat("Parsing", format);
    format.setLenient(false);
    final ParsePosition pos = new ParsePosition(0);
    final Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar)
    if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) {
        String msg = "Error converting '" + toString(sourceType) + "' to '" + toString(targetType) + "'";
        if (format instanceof SimpleDateFormat) {
            msg += " using pattern '" + ((SimpleDateFormat)format).toPattern() + "'";
        }
        if (log().isDebugEnabled()) {
            log().debug("    " + msg);
        }
        throw new ConversionException(msg);
    }
    final Calendar calendar = format.getCalendar();
    return calendar;
}