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

The following examples show how to use com.ibm.icu.text.SimpleDateFormat#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: ForumSubscriptionScenariosNewEntryTest.java    From olat with Apache License 2.0 5 votes vote down vote up
private Date getEventDate(String date) {

        SimpleDateFormat format = new SimpleDateFormat("DD.MM.YYYY HH:mm");
        try {
            return format.parse(date);
        } catch (ParseException e) {
            throw new AssertionError("wrong date format");
        }

    }
 
Example 2
Source File: DataDefinitionSelector.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private String convertDataSetRepresentation( String dsRepresentation,
		int seriesDefinitionIndex )
{
	if ( dsRepresentation != null )
	{
		String[] strTok = ChartUtil.getStringTokens( dsRepresentation );
		StringBuffer sb = new StringBuffer( );
		for ( int i = 0; i < strTok.length; i++ )
		{
			String strDataElement = strTok[i];
			SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy" ); //$NON-NLS-1$
			NumberFormat nf = NumberFormat.getNumberInstance( );

			try
			{
				Date dateElement = sdf.parse( strDataElement );
				dateElement.setTime( dateElement.getTime( )
						+ ( dateElement.getTime( ) * seriesDefinitionIndex )
						/ 10 );
				sb.append( sdf.format( dateElement ) );
			}
			catch ( ParseException e )
			{
				try
				{
					Number numberElement = nf.parse( strDataElement );
					sb.append( numberElement.doubleValue( )
							* ( seriesDefinitionIndex + 1 ) );
				}
				catch ( ParseException e1 )
				{
					e1.printStackTrace( );
				}
			}
			if ( i < strTok.length - 1 )
			{
				sb.append( "," ); //$NON-NLS-1$
			}

		}
		return sb.toString( );
	}
	return null;
}
 
Example 3
Source File: PropertyLocalizationTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Test toDisplayString in English locale.
 * 
 * @throws Exception
 */
public void testToDisplayStringInEn( ) throws Exception
{
    openDesign( "PropertyLocalizationTest.xml", ULocale.ENGLISH ); //$NON-NLS-1$

    // 1. Color
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.COLOR_TYPE );
    assertEquals( "Red", propertyType.toDisplayString( null, null, "red" ) ); //$NON-NLS-1$//$NON-NLS-2$
    assertEquals( "Blue", propertyType.toDisplayString( null, null, "blue" ) ); //$NON-NLS-1$//$NON-NLS-2$
    assertEquals(
            "RGB(255,0,0)", propertyType.toDisplayString( design, null, Integer.decode( "#FF0000" ) ) ); //$NON-NLS-1$//$NON-NLS-2$
    assertEquals(
            "RGB(254,220,186)", propertyType.toDisplayString( design, null, Integer.decode( "#FEDCBA" ) ) ); //$NON-NLS-1$//$NON-NLS-2$

    // Customer color

    assertEquals( "My Color", propertyType.toDisplayString( design, null, //$NON-NLS-1$
            "myColor1" ) ); //$NON-NLS-1$

    // 2. DateTime
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.DATE_TIME_TYPE );
    SimpleDateFormat formatter = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss" ); //$NON-NLS-1$ 
    Date date = formatter.parse( "1981-04-29 06:10:50" ); //$NON-NLS-1$
    assertEquals(
            "4/29/81", propertyType.toDisplayString( null, null, date ) ); //$NON-NLS-1$

    // 3. Number
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.NUMBER_TYPE );
    double d1 = 123456.78d;
    BigDecimal number = new BigDecimal( d1 );
    assertEquals(
            "123,456.78", propertyType.toDisplayString( null, null, number ) ); //$NON-NLS-1$

    // 4. Choice
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.CHOICE_TYPE );

    PropertyDefn propDefn = new SystemPropertyDefn( );
    // Colors:
    propDefn.setDetails( MetaDataDictionary.getInstance( )
            .getPropertyType( PropertyType.COLOR_TYPE ).getChoices( ) );

    assertEquals( "Red", propertyType.toDisplayString( //$NON-NLS-1$
            null, propDefn, "red" ) ); //$NON-NLS-1$

    // fontWeights:
    propDefn.setDetails( MetaDataDictionary.getInstance( ).getChoiceSet(
            "fontWeight" ) ); //$NON-NLS-1$
    assertEquals(
            "Bold", propertyType.toDisplayString( null, propDefn, "bold" ) ); //$NON-NLS-1$//$NON-NLS-2$

    // 5. Boolean
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.BOOLEAN_TYPE );
    assertEquals(
            "true", propertyType.toDisplayString( null, null, new Boolean( true ) ) ); //$NON-NLS-1$
    assertEquals(
            "false", propertyType.toDisplayString( null, null, new Boolean( false ) ) ); //$NON-NLS-1$

    // 6. Float

    // 7. Dimension
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.DIMENSION_TYPE );
    DimensionValue dimValue = new DimensionValue( 123456.78d,
            DesignChoiceConstants.UNITS_CM );
    assertEquals(
            "123,456.78cm", propertyType.toDisplayString( null, null, dimValue ) ); //$NON-NLS-1$

}
 
Example 4
Source File: PropertyLocalizationTest.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Test toDisplayString() in testing locale.
 * 
 * @throws ParseException
 * 
 * @throws DesignFileException
 */
public void testToDisplayStringInTestingULocale( ) throws ParseException,
        DesignFileException
{
    openDesign( "PropertyLocalizationTest.xml", TEST_LOCALE ); //$NON-NLS-1$

    // 1. Color
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.COLOR_TYPE );
    assertEquals(
            "\u7ea2\u8272", propertyType.toDisplayString( null, null, "red" ) ); //$NON-NLS-1$//$NON-NLS-2$
    assertEquals(
            "\u84dd\u8272", propertyType.toDisplayString( null, null, "blue" ) ); //$NON-NLS-1$//$NON-NLS-2$
    assertEquals(
            "RGB(255,0,0)", propertyType.toDisplayString( design, null, Integer.decode( "#FF0000" ) ) ); //$NON-NLS-1$//$NON-NLS-2$

    // 2. Boolean
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.BOOLEAN_TYPE );
    assertEquals(
            "\u771f", propertyType.toDisplayString( null, null, new Boolean( true ) ) ); //$NON-NLS-1$
    assertEquals(
            "\u5047", propertyType.toDisplayString( null, null, new Boolean( false ) ) ); //$NON-NLS-1$

    // message.properties unrelated case.

    ThreadResources.setLocale( ULocale.CHINA );

    // 3. DateTime
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.DATE_TIME_TYPE );
    SimpleDateFormat formatter = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss" ); //$NON-NLS-1$ 
    Date date = formatter.parse( "1981-04-29 06:10:50" ); //$NON-NLS-1$

    // The icu version change cause this to fail, change the input string
    // (previous was "81-4-29") to accommodate
    assertEquals(
            "81/4/29", propertyType.toDisplayString( null, null, date ) ); //$NON-NLS-1$

    // 4. Number
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.NUMBER_TYPE );
    double d = 123456.78d;
    BigDecimal number = new BigDecimal( d );
    assertEquals(
            "123,456.78", propertyType.toDisplayString( null, null, number ) ); //$NON-NLS-1$

    // 5. Float
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.FLOAT_TYPE );
    double d1 = 123456.78d;
    Double number1 = new Double( d1 );
    assertEquals(
            "123,456.78", propertyType.toDisplayString( null, null, number1 ) ); //$NON-NLS-1$

    // 6. Dimension
    propertyType = MetaDataDictionary.getInstance( ).getPropertyType(
            PropertyType.DIMENSION_TYPE );
    DimensionValue dimValue = new DimensionValue( 123456.78d,
            DesignChoiceConstants.UNITS_CM );
    assertEquals(
            "123,456.78cm", propertyType.toDisplayString( null, null, dimValue ) ); //$NON-NLS-1$

    dimValue = new DimensionValue( 123456.78d, "mm" ); //$NON-NLS-1$
    assertEquals(
            "123,456.78mm", propertyType.toDisplayString( null, null, dimValue ) ); //$NON-NLS-1$
}
 
Example 5
Source File: FormatterImpl.java    From org.openntf.domino with Apache License 2.0 4 votes vote down vote up
public Calendar parseDateToCalWithFormat(final String image, final String format, final boolean[] noDT, final boolean parseLenient) {
	Calendar ret = getInstance(iLocale);
	ret.setLenient(false);
	ParsePosition p = new ParsePosition(0);
	ret.clear();
	SimpleDateFormat sdf = new SimpleDateFormat(format, iLocale);
	sdf.parse(image, ret, p);
	boolean contDate = ret.isSet(YEAR) || ret.isSet(MONTH) || ret.isSet(DAY_OF_MONTH);
	boolean contTime = ret.isSet(HOUR_OF_DAY) || ret.isSet(HOUR) || ret.isSet(MINUTE) || ret.isSet(SECOND);
	boolean illegalDateString = !contDate && !contTime;
	if (!illegalDateString && !parseLenient) {
		int lh = image.length();
		int errInd = p.getErrorIndex();
		illegalDateString = (errInd < 0 && p.getIndex() < lh) || (errInd >= 0 && errInd < lh);
	}
	if (illegalDateString)
		throw new IllegalArgumentException("Illegal date string '" + image + "' for format '" + format + "'");
	//		System.out.println("Y=" + ret.isSet(YEAR) + " M=" + ret.isSet(MONTH) + " D=" + ret.isSet(DAY_OF_MONTH)
	//				+ " H=" + ret.isSet(HOUR_OF_DAY) + "m=" + ret.isSet(MINUTE) + " S=" + ret.isSet(SECOND));
	if (!ret.isSet(YEAR))
		ret.set(YEAR, 1970);
	if (!ret.isSet(MONTH))
		ret.set(MONTH, 0);
	if (!ret.isSet(DAY_OF_MONTH))
		ret.set(DAY_OF_MONTH, 1);
	if (!ret.isSet(HOUR_OF_DAY) && !ret.isSet(HOUR))
		ret.set(HOUR_OF_DAY, 0);
	if (!ret.isSet(MINUTE))
		ret.set(MINUTE, 0);
	if (!ret.isSet(SECOND))
		ret.set(SECOND, 0);
	try {
		ret.getTime();
	} catch (IllegalArgumentException e) {
		throw new IllegalArgumentException("Parsing '" + image + "' against '" + format + "' gives Calendar exception: "
				+ e.getMessage());
	}
	noDT[0] = !contDate;
	noDT[1] = !contTime;
	return ret;
}