Java Code Examples for com.ibm.icu.text.DateFormat#parse()

The following examples show how to use com.ibm.icu.text.DateFormat#parse() . 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: DateFormatISO8601.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parse a date/time string.
 *
 * @param source
 * @return
 * @throws ParseException
 */
public static Date parse( String source, TimeZone timeZone ) throws BirtException,
		ParseException
{
	DateFormat dateFormat = getSimpleDateFormat( source, timeZone );
	Date resultDate = null;
	try
	{
		if ( timeZone != null )
		{
			dateFormat.setTimeZone( timeZone );
		}
		if ( dateFormat != null )
		{
			source = cleanDate( source );
			resultDate = dateFormat.parse( source );
		}
		return resultDate;
	}
	catch ( ParseException e )
	{
		throw new CoreException( ResourceConstants.CONVERT_FAILS,
				new Object[]{source.toString( ), "Date"} );
	}
}
 
Example 2
Source File: DataTypeUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses a date/time string
 *
 * @param source
 * @param locale
 * @param timeZone
 * @return
 * @throws BirtException
 */
public static Date toDate( String source, ULocale locale, TimeZone timeZone )
		throws BirtException
{
	DateFormat dateFormat = getDateFormatObject( source, locale, timeZone );
	Date resultDate = null;
	try
	{
		resultDate = dateFormat.parse( source );
		return resultDate;
	}
	catch ( ParseException e )
	{
		throw new CoreException(
				ResourceConstants.CONVERT_FAILS,
				new Object[]{
						source.toString( ), "Date"
				} );
	}
}
 
Example 3
Source File: DateTimePropertyType.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Validates the locale-dependent value for the date time type, validate the
 * <code>value</code> in the locale-dependent way and convert the
 * <code>value</code> into a Date object.
 * 
 * @return object of type Date or null if <code>value</code> is null.
 */

public Object validateInputString( Module module, DesignElement element,
		PropertyDefn defn, String value ) throws PropertyValueException
{
	if ( StringUtil.isBlank( value ) )
	{
		return null;
	}

	// Parse the input in locale-dependent way.
	ULocale locale = module == null ? ThreadResources.getLocale( ) : module
			.getLocale( );
	DateFormat formatter = DateFormat.getDateInstance( DateFormat.SHORT,
			locale );
	try
	{
		return formatter.parse( value );
	}
	catch ( ParseException e )
	{
		logger.log( Level.SEVERE, "Invalid date value:" + value ); //$NON-NLS-1$
		throw new PropertyValueException( value,
				PropertyValueException.DESIGN_EXCEPTION_INVALID_VALUE,
				DATE_TIME_TYPE );
	}
}
 
Example 4
Source File: DataTypeUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Convert string to date with check.
 * JDK may do incorrect converse, for example:
 * 		2005/1/1 Local.US, format pattern is MM/dd/YY.
 * Above conversion can be done without error, but obviously
 * the result is not right. This method will do such a simple check,
 * in DateFormat.SHORT case instead of all cases.
 * 		Year is not lower than 0.
 * 		Month is from 1 to 12.
 * 		Day is from 1 to 31.  
 * @param source
 * @param locale
 * @return Date
 * @throws BirtException
 */
public static Date toDateWithCheck( String source, ULocale locale )
		throws BirtException
{
	DateFormat dateFormat = DateFormatFactory.getDateInstance( DateFormat.SHORT,
			locale );
	Date resultDate = null;
	try
	{
		resultDate = dateFormat.parse( source );
	}
	catch ( ParseException e )
	{
		return toDate( source, locale );
	}

	// check whether conversion is correct
	if ( DateUtil.checkValid( dateFormat, source ) == false )
	{
		throw new CoreException( 
				ResourceConstants.CONVERT_FAILS,
				new Object[]{
						source.toString( ), "Date"
				});
	}

	return resultDate;
}
 
Example 5
Source File: DominoFormatter.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the date from string.
 * 
 * @param dateString
 *            the date string
 * @return the date
 */
public Date parseDateFromString(final String dateString) {
	DateFormat df = getDateTimeFormat();
	synchronized (df) {
		try {
			return df.parse(dateString);
		} catch (ParseException e) {
			DominoUtils.handleException(e);
			return null;
		}
	}
}