当前位置: 首页>>代码示例>>C++>>正文


C++ SimpleDateFormat::applyPattern方法代码示例

本文整理汇总了C++中SimpleDateFormat::applyPattern方法的典型用法代码示例。如果您正苦于以下问题:C++ SimpleDateFormat::applyPattern方法的具体用法?C++ SimpleDateFormat::applyPattern怎么用?C++ SimpleDateFormat::applyPattern使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SimpleDateFormat的用法示例。


在下文中一共展示了SimpleDateFormat::applyPattern方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: pp

/**
 * @bug 4104522
 * CANNOT REPRODUCE
 * According to the bug report, this test should throw a
 * StringIndexOutOfBoundsException during the second parse.  However,
 * this is not seen.
 */
void DateFormatRegressionTest::Test4104522(void) 
{
    UErrorCode status = U_ZERO_ERROR;
    
    SimpleDateFormat *sdf = new SimpleDateFormat(status);
    if(U_FAILURE(status)) {
      dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
      delete sdf;
      return;
    }
    failure(status, "new SimpleDateFormat");
    UnicodeString pattern = "'time' hh:mm";
    sdf->applyPattern(pattern);
    logln("pattern: \"" + pattern + "\"");

    // works correctly
    ParsePosition pp(0);
    UnicodeString text = "time ";
    UDate dt = sdf->parse(text, pp);
    logln(" text: \"" + text + "\"" +
          " date: " + dt);

    // works wrong
    pp.setIndex(0);
    text = "time";
    dt = sdf->parse(text, pp);
    logln(" text: \"" + text + "\"" +
          " date: " + dt);

    delete sdf;
}
开发者ID:Abocer,项目名称:android-4.2_r1,代码行数:38,代码来源:dtfmrgts.cpp

示例2: pos

/**
 * @bug 4104136
 */
void DateFormatRegressionTest::Test4104136(void) 
{
    UErrorCode status = U_ZERO_ERROR;
    SimpleDateFormat *sdf = new SimpleDateFormat(status); 
    if(U_FAILURE(status)) {
      dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
      delete sdf;
      return;
    }
    if(failure(status, "new SimpleDateFormat")) return;
    UnicodeString pattern = "'time' hh:mm"; 
    sdf->applyPattern(pattern); 
    logln("pattern: \"" + pattern + "\""); 

    UnicodeString strings [] = {
        (UnicodeString)"time 10:30",
        (UnicodeString) "time 10:x",
        (UnicodeString) "time 10x"
    };

    ParsePosition ppos [] = {
        ParsePosition(10),
        ParsePosition(0),
        ParsePosition(0)
    };

    UDate dates [] = {
        date(70, UCAL_JANUARY, 1, 10, 30),
        -1,
        -1
    };

    /*Object[] DATA = {
        "time 10:30", new ParsePosition(10), new Date(70, Calendar.JANUARY, 1, 10, 30),
        "time 10:x", new ParsePosition(0), null,
        "time 10x", new ParsePosition(0), null,
    };*/
    
    for(int i = 0; i < 3; i++) {
        UnicodeString text = strings[i];
        ParsePosition finish = ppos[i];
        UDate exp = dates[i];
        
        ParsePosition pos(0);
        UDate d = sdf->parse(text, pos);
        logln(" text: \"" + text + "\""); 
        logln(" index: %d", pos.getIndex()); 
        logln((UnicodeString) " result: " + d);
        if(pos.getIndex() != finish.getIndex())
            errln("Fail: Expected pos " + finish.getIndex());
        if (! ((d == 0 && exp == -1) || (d == exp)))
            errln((UnicodeString) "Fail: Expected result " + exp);
    }

    delete sdf;
}
开发者ID:Abocer,项目名称:android-4.2_r1,代码行数:59,代码来源:dtfmrgts.cpp

示例3: replaceFieldTypesExample

static void replaceFieldTypesExample() {
		// Use repalceFieldTypes API to replace zone 'zzzz' with 'vvvv'
       u_printf("========================================================================\n");
       u_printf(" replaceFieldTypeExample()\n");
       u_printf("\n");
       u_printf(" Use replaceFieldTypes API to replace zone 'zzzz' with 'vvvv'\n");
       u_printf("========================================================================\n");
	   //! [replaceFieldTypesExample]
		UFILE *out = u_finit(stdout, NULL, "UTF-8");
		UErrorCode status =U_ZERO_ERROR;
		UnicodeString pattern,dateReturned;
		Locale locale =Locale::getFrance();
		Calendar *cal = Calendar::createInstance(status);
		cal->set (1999,9,13,23,58,59);
		UDate date = cal->getTime(status);
		TimeZone *zone = TimeZone::createTimeZone(UnicodeString("Europe/Paris"));
		DateTimePatternGenerator *dtfg = DateTimePatternGenerator::createInstance(locale,status);
	    SimpleDateFormat *sdf = new SimpleDateFormat("EEEE d MMMM y HH:mm:ss zzzz",locale,status);
		sdf->setTimeZone(*zone);
		pattern = sdf->toPattern(pattern);
		u_fprintf(out, "%S\n", UnicodeString("Pattern before replacement:").getTerminatedBuffer());
      	u_fprintf(out, "%S\n", pattern.getTerminatedBuffer());
		dateReturned.remove();
		dateReturned = sdf->format(date, dateReturned, status);
		u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
		u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
        // Replace zone "zzzz" in the pattern with "vvvv"
		UnicodeString newPattern = dtfg->replaceFieldTypes(pattern, "vvvv", status);
		// Apply the new pattern
		sdf->applyPattern(newPattern);
		dateReturned.remove();
		dateReturned = sdf->format(date, dateReturned, status);
		u_fprintf(out, "%S\n", UnicodeString("Pattern after replacement:").getTerminatedBuffer());
     	u_fprintf(out, "%S\n", newPattern.getTerminatedBuffer());
     	u_fprintf(out, "%S\n", UnicodeString("Date/Time format in fr_FR:").getTerminatedBuffer());
		u_fprintf(out, "%S\n", dateReturned.getTerminatedBuffer());
		delete sdf;
		delete dtfg;
		delete zone;
		delete cal;
		u_fclose(out);

		//! [replaceFieldTypesExample]
		/* output of the sample code:
        *************************************************************************************************
         Pattern before replacement:
         EEEE d MMMM y HH:mm:ss zzzz
         Date/Time format in fr_FR:
         jeudi 14 octobre 1999 05:58:59 heure avancée d’Europe centrale
         Pattern after replacement:
         EEEE d MMMM y HH:mm:ss vvvv
         Date/Time format in fr_FR:
         jeudi 14 octobre 1999 05:58:59 heure de l’Europe centrale

        *************************************************************************************************/
    }
开发者ID:Distrotech,项目名称:icu,代码行数:56,代码来源:dtptngsample.cpp

示例4: pattern

void
BCountry::SetTimeFormat(const char* formatString, bool longFormat)
{
    icu_4_2::DateFormat* dateFormatter
        = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
    SimpleDateFormat* dateFormatterImpl
        = static_cast<SimpleDateFormat*>(dateFormatter);

    UnicodeString pattern(formatString);
    dateFormatterImpl->applyPattern(pattern);
}
开发者ID:mmadia,项目名称:haiku-1,代码行数:11,代码来源:Country.cpp

示例5:

void
RelativeDateFormat::applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status)
{
    if (!U_FAILURE(status)) {
        SimpleDateFormat* sdtfmt = NULL;
        SimpleDateFormat* stmfmt = NULL;
        if (fDateFormat && (sdtfmt = dynamic_cast<SimpleDateFormat*>(fDateFormat)) == NULL) {
            status = U_UNSUPPORTED_ERROR;
            return;
        }
        if (fTimeFormat && (stmfmt = dynamic_cast<SimpleDateFormat*>(fTimeFormat)) == NULL) {
            status = U_UNSUPPORTED_ERROR;
            return;
        }
        if ( fDateFormat ) {
            sdtfmt->applyPattern(datePattern);
        }
        if ( fTimeFormat ) {
            stmfmt->applyPattern(timePattern);
        }
    }
}
开发者ID:0x4d52,项目名称:JavaScriptCore-X,代码行数:22,代码来源:reldtfmt.cpp

示例6: TestPersianFormat

void IntlCalendarTest::TestPersianFormat() {
    UErrorCode status = U_ZERO_ERROR;
    SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale(" [email protected]=persian"), status);
    CHECK(status, "creating date format instance");
    SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=gregorian"), status);
    CHECK(status, "creating gregorian date format instance");
    UnicodeString gregorianDate("January 18, 2007 AD");
    UDate aDate = fmt2->parse(gregorianDate, status); 
    if(!fmt) { 
        errln("Coudln't create en_US instance");
    } else {
        UnicodeString str;
        fmt->format(aDate, str);
        logln(UnicodeString() + "as Persian Calendar: " + escape(str));
        UnicodeString expected("Dey 28, 1385 AP");
        if(str != expected) {
            errln("Expected " + escape(expected) + " but got " + escape(str));
        }
        UDate otherDate = fmt->parse(expected, status); 
        if(otherDate != aDate) { 
            UnicodeString str3;
            fmt->format(otherDate, str3);
            errln("Parse incorrect of " + escape(expected) + " - wanted " + aDate + " but got " +  otherDate + ", " + escape(str3)); 
        } else {
            logln("Parsed OK: " + expected);
        }
        // Two digit year parsing problem #4732
        fmt->applyPattern("yy-MM-dd");
        str.remove();
        fmt->format(aDate, str);
        expected.setTo("85-10-28");
        if(str != expected) {
            errln("Expected " + escape(expected) + " but got " + escape(str));
        }
        otherDate = fmt->parse(expected, status);
        if (otherDate != aDate) {
            errln("Parse incorrect of " + escape(expected) + " - wanted " + aDate + " but got " + otherDate); 
        } else {
            logln("Parsed OK: " + expected);
        }
        delete fmt;
    }
    delete fmt2;
    
    CHECK(status, "Error occured testing Persian Calendar in English "); 
}
开发者ID:ilseokhan,项目名称:platform_external_icu4c,代码行数:46,代码来源:incaltst.cpp

示例7: pattern

DateFormat*
BLocale::_CreateTimeFormatter(const BString& format) const
{
	Locale* icuLocale
		= fConventions.UseStringsFromPreferredLanguage()
			? BLanguage::Private(&fLanguage).ICULocale()
			: BFormattingConventions::Private(&fConventions).ICULocale();

	DateFormat* timeFormatter
		= DateFormat::createTimeInstance(DateFormat::kShort, *icuLocale);
	if (timeFormatter == NULL)
		return NULL;

	SimpleDateFormat* timeFormatterImpl
		= static_cast<SimpleDateFormat*>(timeFormatter);

	UnicodeString pattern(format.String());
	timeFormatterImpl->applyPattern(pattern);

	return timeFormatter;
}
开发者ID:Barrett17,项目名称:haiku-contacts-kit-old,代码行数:21,代码来源:Locale.cpp

示例8: addPatternExample

static void addPatternExample() {
		
		u_printf("========================================================================\n");
        u_printf(" addPatternExample()\n");
		u_printf("\n");
        u_printf(" Use addPattern API to add new '. von' to existing pattern\n");
        u_printf("========================================================================\n");
		//! [addPatternExample]
		UErrorCode status =U_ZERO_ERROR;
		UnicodeString conflictingPattern,dateReturned, pattern;
		Locale locale=Locale::getFrance();
		Calendar *cal = Calendar::createInstance(status);
		cal->set (1999,9,13,23,58,59);
		UDate date = cal->getTime(status);
        // Create an DateTimePatternGenerator instance for the given locale
		DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locale,status);
		SimpleDateFormat *sdf = new SimpleDateFormat(dtfg->getBestPattern("MMMMddHmm",status),locale,status);
        // Add '. von' to the existing pattern
        dtfg->addPattern("dd'. von' MMMM", true, conflictingPattern,status);
        // Apply the new pattern
        sdf->applyPattern(dtfg->getBestPattern("MMMMddHmm",status));
		dateReturned = sdf->format(date, dateReturned, status);
		pattern =sdf->toPattern(pattern);
		u_printf("%s\n", "New Pattern for FRENCH: ");
      	u_printf("%S\n", pattern.getTerminatedBuffer());
		u_printf("%s\n", "Date Time in new Pattern: ");
		u_printf("%S\n", dateReturned.getTerminatedBuffer());
		delete dtfg;
		delete sdf;
		delete cal;

		//! [addPatternExample]
        /* output of the sample code:
        ************************************************************************************************
         New Pattern for FRENCH: dd. 'von' MMMM HH:mm
         Date Time in new Pattern: 13. von octobre 23:58
     
        *************************************************************************************************/
 	}
开发者ID:CODECOMMUNITY,项目名称:rust-icu,代码行数:39,代码来源:dtptngsample.cpp

示例9: testAPI


//.........这里部分代码省略.........
        return;
    }
    UnicodeString findPattern = gen->getBestPattern(UnicodeString("MMMddHmm"), status);
    SimpleDateFormat *format = new SimpleDateFormat(findPattern, deLocale, status);
    if (U_FAILURE(status)) {
        dataerrln("ERROR: Could not create SimpleDateFormat (Locale::getGermany())");
        delete gen;
        return;
    }
    TimeZone *zone = TimeZone::createTimeZone(UnicodeString("ECT"));
    if (zone==NULL) {
        dataerrln("ERROR: Could not create TimeZone ECT");
        delete gen;
        delete format;
        return;
    }
    format->setTimeZone(*zone);
    UnicodeString dateReturned, expectedResult;
    dateReturned.remove();
    dateReturned = format->format(sampleDate, dateReturned, status);
    expectedResult=UnicodeString("14. Okt 8:58", -1, US_INV);
    if ( dateReturned != expectedResult ) {
        errln("ERROR: Simple test in getBestPattern with Locale::getGermany()).");
    }
    // add new pattern
    status = U_ZERO_ERROR;
    conflictingStatus = gen->addPattern(UnicodeString("d'. von' MMMM", -1, US_INV), true, conflictingPattern, status); 
    if (U_FAILURE(status)) {
        errln("ERROR: Could not addPattern - d\'. von\' MMMM");
    }
    status = U_ZERO_ERROR;
    UnicodeString testPattern=gen->getBestPattern(UnicodeString("MMMMdd"), status);
    testPattern=gen->getBestPattern(UnicodeString("MMMddHmm"), status);
    format->applyPattern(gen->getBestPattern(UnicodeString("MMMMddHmm"), status));
    dateReturned.remove();
    dateReturned = format->format(sampleDate, dateReturned, status);
    expectedResult=UnicodeString("14. von Oktober 8:58", -1, US_INV);
    if ( dateReturned != expectedResult ) {
        errln("ERROR: Simple test addPattern failed!: d\'. von\' MMMM  ");
    }
    delete format;
    
    // get a pattern and modify it
    format = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull, 
                                                                  deLocale);
    format->setTimeZone(*zone);
    UnicodeString pattern;
    pattern = format->toPattern(pattern);
    dateReturned.remove();
    dateReturned = format->format(sampleDate, dateReturned, status);
    expectedResult=CharsToUnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Mitteleurop\\u00E4ische Sommerzeit");
    if ( dateReturned != expectedResult ) {
        errln("ERROR: Simple test uses full date format.");
        errln(UnicodeString(" Got: ") + dateReturned + UnicodeString(" Expected: ") + expectedResult);
    }
     
    // modify it to change the zone.  
    UnicodeString newPattern = gen->replaceFieldTypes(pattern, UnicodeString("vvvv"), status);
    format->applyPattern(newPattern);
    dateReturned.remove();
    dateReturned = format->format(sampleDate, dateReturned, status);
    expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich");
    if ( dateReturned != expectedResult ) {
        errln("ERROR: Simple test modify the timezone!");
        errln(UnicodeString(" Got: ")+ dateReturned + UnicodeString(" Expected: ") + expectedResult);
    }
开发者ID:Andproject,项目名称:platform_external_icu4c,代码行数:67,代码来源:dtptngts.cpp


注:本文中的SimpleDateFormat::applyPattern方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。