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

The following examples show how to use java.text.DateFormat#getCalendar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Static method to get date-time DateFormat for input.  This is based on a 2 digit year
 * input mask, which also handles 4-digit year, but caller must use doParseDate() to
 * handle single-digit years, out-of-bounds years, and trailing garbage in input string.
 * Also used by FilterItem to get DateFormat to store report dates in US locale.
 * 
 * @param locale locale
 * @param tz time zone
 * @return a date and time DateFormat.
 */
public static DateFormat getLocaleInputDateTimeFormat(Locale locale, TimeZone tz) {
    DateFormat df = getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);

    df.setLenient(false);
    df.setTimeZone(tz);
    Calendar calendar = df.getCalendar();
    calendar.set(Calendar.YEAR, 1959);  // 60 means 1960, 59 means 2059
    calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
    
    assert (df instanceof SimpleDateFormat);
    ((SimpleDateFormat)df).set2DigitYearStart(calendar.getTime());
    return df;
}
 
Example 2
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Parse the given input string using the given format, and make sure the entire string
 * is used up during the parsing and garbage text at the end is not allowed.  Also
 * handles single-digit years and verify out-of-bounds years.  All date parsing in
 * the app must go through this.
 * 
 * @param input date string
 * @param df DateFormat used to parse the date
 * @return a parsed date.
 */
public static Date doParseDate(String input, DateFormat df) throws ParseException {
    Date date = doParseTime(input, df);
    
    // Handles dates that are entered with one or two digit years
    Calendar cal = df.getCalendar();
    cal.setTime(date);
    int year = cal.get(Calendar.YEAR);
    if (year >= 0 && year < 60) {
        cal.set(Calendar.YEAR, 2000 + year);
    } else if (year >= 60 && year < 100) {
        cal.set(Calendar.YEAR, 1900 + year);
    }

    // Verify out-of-bounds years
    date = cal.getTime();
    if (date.before(EARLIEST) || date.after(LATEST)) {
        throw new ParseException("Invalid year", 0);
    }
    return date;
}
 
Example 3
Source File: DateTimeConverter.java    From Oceanus with Apache License 2.0 6 votes vote down vote up
/**
 * 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(Class<?> sourceType, Class<?> targetType, String value, DateFormat format) {
    logFormat("Parsing", format);
    format.setLenient(false);
    ParsePosition pos = new ParsePosition(0);
    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);
    }
    Calendar calendar = format.getCalendar();
    return calendar;
}
 
Example 4
Source File: DateTimeConverter.java    From commons-beanutils with Apache License 2.0 6 votes vote down vote up
/**
 * Parse a String into a {@code Calendar} object
 * using the specified {@code DateFormat}.
 *
 * @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;
}
 
Example 5
Source File: DateConverterTestBase.java    From commons-beanutils with Apache License 2.0 6 votes vote down vote up
/**
 * Parse a String value to a Calendar
 * @param value The String value to parse
 * @param pattern The date pattern
 * @param locale The locale to use (or null)
 * @return parsed Calendar value
 */
Calendar toCalendar(final String value, final String pattern, final Locale locale) {
    Calendar calendar = null;
    try {
        final DateFormat format = locale == null
                       ? new SimpleDateFormat(pattern)
                       : new SimpleDateFormat(pattern, locale);
        format.setLenient(false);
        format.parse(value);
        calendar = format.getCalendar();
    } catch (final Exception e) {
        fail("Error creating Calendar value ='"
                + value + ", pattern='" + pattern + "' " + e.toString());
    }
    return calendar;
}
 
Example 6
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Static method to get date-only DateFormat for input. This is based on a 2 digit year
 * input mask, which also handles 4-digit year, but caller must use doParseDate() to
 * handle single-digit years, out-of-bounds years, and trailing garbage in input string.
 * Also used by FilterItem to get DateFormat to store report dates in US locale.
 * 
 * @param locale locale
 * @param tz time zone
 * @return a date-only DateFormat.
 */
public static DateFormat getLocaleInputDateFormat(Locale locale, TimeZone tz) {
    DateFormat df = getFormatProvider(locale).getDateInstance(DateFormat.SHORT, locale);

    df.setLenient(false);
    df.setTimeZone(tz);
    Calendar calendar = df.getCalendar();
    calendar.set(Calendar.YEAR, 1959);  // 60 means 1960, 59 means 2059; handle potential daylight saving difference
    calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
    ((SimpleDateFormat)df).set2DigitYearStart(calendar.getTime());
    return df;
}
 
Example 7
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Static method to get date-only DateFormat for input based on style. This is based on a 2 digit year
 * input mask, which also handles 4-digit year, but caller must use doParseDate() to
 * handle single-digit years, out-of-bounds years, and trailing garbage in input string.
 * 
 * @param locale locale
 * @param style DateFormat style
 * @param tz time zone
 * @return a date-only DateFormat.
 */
public static DateFormat getLocaleInputDateFormat(Locale locale, int style, TimeZone tz) {   	    
    DateFormat df ;   
    
    switch (style) {
        case DateFormat.SHORT :
            df = getFormatProvider(locale).getDateInstance(DateFormat.SHORT, locale);
            break;
        case DateFormat.MEDIUM :
            df = getFormatProvider(locale).getDateInstance(DateFormat.MEDIUM, locale);
            break;
        case DateFormat.LONG :
            df = getFormatProvider(locale).getDateInstance(DateFormat.LONG, locale);
            break;
        default :
            df = getFormatProvider(locale).getDateInstance(DateFormat.SHORT, locale);
    }
    
    df.setLenient(false);
    df.setTimeZone(tz);
    Calendar calendar = df.getCalendar();
    calendar.set(Calendar.YEAR, 1959);  // 60 means 1960, 59 means 2059; handle potential daylight saving difference
    calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
    ((SimpleDateFormat)df).set2DigitYearStart(calendar.getTime());
    return df;
}
 
Example 8
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Static method to get date-time DateFormat for input.  This is based on a 2 digit year
 * input mask, which also handles 4-digit year, but caller must use doParseDate() to
 * handle single-digit years, out-of-bounds years, and trailing garbage in input string.
 * DateFormat.LONG uses short-date and long-time formats
 * 
 * @param locale locale
 * @param tz time zone
 * @param style DateFormat style
 * @return a date and time DateFormat.
 */

public static DateFormat getLocaleInputDateTimeFormat(Locale locale, int style, TimeZone tz) {
	    DateFormat df;
	    switch (style) {
        case DateFormat.SHORT :
            df= getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
            break;
        case DateFormat.MEDIUM :
            df = getFormatProvider(locale).getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
            break;
        case DateFormat.LONG :
            df = getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG, locale);
            break;
        default :
            df = getFormatProvider(locale).getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
	    
	    }
	    	 
    df.setLenient(false);
    df.setTimeZone(tz);
    Calendar calendar = df.getCalendar();
    calendar.set(Calendar.YEAR, 1959);  // 60 means 1960, 59 means 2059
    calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
    
    assert (df instanceof SimpleDateFormat);
    ((SimpleDateFormat)df).set2DigitYearStart(calendar.getTime());
    return df;
}
 
Example 9
Source File: BaseLocalizer.java    From grammaticus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Static method to get DateFormat for time input based on style
 * Caller must do doParseTime for parsing
 * @param locale locale
 * @param style DateFormat style
 * @param tz time zone
 * @return a date-only DateFormat.
 */
public static DateFormat getLocaleInputTimeFormat(Locale locale, int style, TimeZone tz) {   	    
    DateFormat df ;   
    
    switch (style) {
        case DateFormat.SHORT :
            df = getFormatProvider(locale).getTimeInstance(DateFormat.SHORT, locale);
            break;
        case DateFormat.MEDIUM :
            df = getFormatProvider(locale).getTimeInstance(DateFormat.MEDIUM, locale);
            break;
        case DateFormat.LONG :
            df = getFormatProvider(locale).getTimeInstance(DateFormat.LONG, locale);
            break;
        default :
            df = getFormatProvider(locale).getTimeInstance(DateFormat.SHORT, locale);
    }
    
    df.setLenient(false);
    df.setTimeZone(tz);
    Calendar calendar = df.getCalendar();
    calendar.set(Calendar.YEAR, 1959);  // 60 means 1960, 59 means 2059; handle potential daylight saving difference
    calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));
    ((SimpleDateFormat)df).set2DigitYearStart(calendar.getTime());
    return df;
}
 
Example 10
Source File: DateTimeConverter.java    From hasor with Apache License 2.0 5 votes vote down vote up
/**
 * 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) {
    format.setLenient(false);
    ParsePosition pos = new ParsePosition(0);
    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 '" + this.toString(sourceType) + "' to '" + this.toString(targetType) + "'";
        if (format instanceof SimpleDateFormat) {
            msg += " using pattern '" + ((SimpleDateFormat) format).toPattern() + "'";
        }
        throw new ConversionException(msg);
    }
    Calendar calendar = format.getCalendar();
    return calendar;
}
 
Example 11
Source File: DateConverter.java    From reflectutils with Apache License 2.0 5 votes vote down vote up
/**
 * Parse a String into a <code>Calendar</code> object using the specified <code>DateFormat</code><br/>
 * Does exact matching (non-lenient) only on the given pattern<br/>
 *
 * @param value The String date value.
 * @param format The DateFormat to parse the String value.
 * @return The converted Calendar object OR null if it cannot be converted
 */
protected static Calendar parse(String value, DateFormat format) {
    format.setLenient(false);
    ParsePosition pos = new ParsePosition(0);
    Date parsedDate = format.parse(value, pos); // ignore the result (use the Calendar)
    Calendar calendar = null;
    if (pos.getErrorIndex() >= 0 || pos.getIndex() != value.length() || parsedDate == null) {
        calendar = null;
    } else {
        calendar = format.getCalendar();
    }
    return calendar;
}
 
Example 12
Source File: DateFormatTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @tests java.text.DateFormat#getCalendar()
 */
public void test_getCalendar() {
	DateFormat format = DateFormat.getInstance();
	Calendar cal1 = format.getCalendar();
	Calendar cal2 = format.getCalendar();
	assertTrue("Calendars not identical", cal1 == cal2);
}
 
Example 13
Source File: XmppDateTime.java    From jxmpp with Apache License 2.0 5 votes vote down vote up
private static Calendar parseXEP91Date(String stampString, DateFormat dateFormat) {
	try {
		dateFormat.parse(stampString);
		return dateFormat.getCalendar();
	} catch (ParseException e) {
		return null;
	}
}
 
Example 14
Source File: ConversionUtils.java    From velocity-tools with Apache License 2.0 4 votes vote down vote up
TimeZoneIDSuffixFormat(DateFormat wrappedFormat)
{
    this.wrappedFormat = wrappedFormat;
    this.calendar = wrappedFormat.getCalendar();
    this.numberFormat = wrappedFormat.getNumberFormat();
}