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

The following examples show how to use com.ibm.icu.text.DateFormat#setTimeZone() . 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: DateFormatWrapperFactory.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
public String format( Date date )
{
	StringBuffer str = new StringBuffer( );
	FieldPosition pos = new FieldPosition( DateFormat.DATE_FIELD );
	DateFormat df = DateFormat.getDateInstance( DateFormat.MEDIUM, locale );
	if ( tz != null )
	{
		df.setTimeZone( tz );
	}
	df.format( date, str, pos );
	int endIndex;
	if ( pos.getEndIndex( ) >= str.length( ) )
	{
		endIndex = pos.getEndIndex( );
	}
	else
	{
		endIndex = pos.getEndIndex( )
				+ ( str.charAt( pos.getEndIndex( ) ) == ',' ? 2 : 1 );
	}
	if ( endIndex >= str.length( ) ) // means date is the last one, need
										// to remove separator
	{
		endIndex = pos.getBeginIndex( );
		while ( endIndex > 0 )
		{
			char ch = str.charAt( endIndex - 1 );
			if ( ch == ' '
					|| ch == ',' || ch == '/' || ch == '-' || ch == '.' )
			{
				endIndex--;
			}
			else
			{
				break;
			}
		}
		return str.substring( 0, endIndex );
	}
	return str.substring( 0, pos.getBeginIndex( ) )
			+ str.substring( endIndex );
}