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


C++ SimpleDateFormat类代码示例

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


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

示例1: SimpleDateFormat

/**
 * @bug 4103341
 */
void DateFormatRegressionTest::Test4103341(void) 
{
    TimeZone *saveZone  =TimeZone::createDefault();
    //try {
        
    // {sfb} changed from setDefault to adoptDefault
    TimeZone::adoptDefault(TimeZone::createTimeZone("CST"));
    UErrorCode status = U_ZERO_ERROR;
    SimpleDateFormat *simple = new SimpleDateFormat(UnicodeString("MM/dd/yyyy HH:mm"), status);
    if(U_FAILURE(status)) {
      dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
      delete simple;
      return;
    }
    failure(status, "new SimpleDateFormat");
    TimeZone *temp = TimeZone::createDefault();
    if(simple->getTimeZone() != *temp)
            errln("Fail: SimpleDateFormat not using default zone");
    //}
    //finally {
        TimeZone::adoptDefault(saveZone);
    //}

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

示例2: SimpleTimeZone

/**
 * @bug 4089106
 */
void DateFormatRegressionTest::Test4089106(void) 
{
    TimeZone *def = TimeZone::createDefault();
    //try {
        TimeZone *z = new SimpleTimeZone((int)(1.25 * 3600000), "FAKEZONE");
        TimeZone::setDefault(*z);
        UErrorCode status = U_ZERO_ERROR;
        SimpleDateFormat *f = new SimpleDateFormat(status);
        if(U_FAILURE(status)) {
          dataerrln("Couldn't create SimpleDateFormat, error %s", u_errorName(status));
          delete f;
          delete def;
          delete z;
          return;
        }
        failure(status, "new SimpleDateFormat");
        if (f->getTimeZone()!= *z)
            errln("Fail: SimpleTimeZone should use TimeZone.getDefault()");
        
        //}
    //finally {
        TimeZone::setDefault(*def);
    //}

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

示例3: SimpleDateFormat

std::string L10n::FormatMillisecondsIntoDateString(const UDate& milliseconds, const std::string& formatString) const
{
	UErrorCode status = U_ZERO_ERROR;
	UnicodeString dateString;
	std::string resultString;

	UnicodeString unicodeFormat = UnicodeString::fromUTF8(formatString.c_str());
	SimpleDateFormat* dateFormat = new SimpleDateFormat(unicodeFormat, status);
	if (U_FAILURE(status))
		LOGERROR("Error creating SimpleDateFormat: %s", u_errorName(status));

	const TimeZone* timeZone = TimeZone::getGMT();

	status = U_ZERO_ERROR;
	Calendar* calendar = Calendar::createInstance(*timeZone, currentLocale, status);
	if (U_FAILURE(status))
		LOGERROR("Error creating calendar: %s", u_errorName(status));
   
	dateFormat->adoptCalendar(calendar);
	dateFormat->format(milliseconds, dateString);
	delete dateFormat;

	dateString.toUTF8String(resultString);
	return resultString;
}
开发者ID:Rektosauros,项目名称:0ad,代码行数:25,代码来源:L10n.cpp

示例4: pattern

void DateFormatRegressionTest::Test5554(void)
{
  UErrorCode status = U_ZERO_ERROR;
  UnicodeString pattern("Z","");
  UnicodeString newfoundland("Canada/Newfoundland", "");
  TimeZone *zone = TimeZone::createTimeZone(newfoundland);
  Calendar *cal = new GregorianCalendar(zone, status);
  SimpleDateFormat *sdf = new SimpleDateFormat(pattern,status);
  if (U_FAILURE(status)) {
    dataerrln("Error constructing SimpleDateFormat");
    delete cal;
    delete sdf;
    return;
  }
  cal->set(2007, 1, 14);
  UDate date = cal->getTime(status);
  if (U_FAILURE(status)) {
    errln("Error getting time to format");
    return;
  };
  sdf->adoptCalendar(cal);
  UnicodeString result;
  UnicodeString correct("-0330", "");
  sdf->format(date, result);
  if (result != correct) {
    errln("\nError: Newfoundland Z of Jan 14, 2007 gave '" + result + "', expected '" + correct + "'");
  }
  delete sdf;
}
开发者ID:Abocer,项目名称:android-4.2_r1,代码行数:29,代码来源:dtfmrgts.cpp

示例5: logln

void
IntlTestDateFormat::describeTest()
{
    // Assume it's a SimpleDateFormat and get some info
    SimpleDateFormat *s = (SimpleDateFormat*)fFormat;
    UnicodeString str;
    logln(fTestName + " Pattern " + s->toPattern(str));
}
开发者ID:00zhengfu00,项目名称:third_party,代码行数:8,代码来源:tsdate.cpp

示例6: getBestPatternExample

static void getBestPatternExample() {
	    
		u_printf("========================================================================\n");
		u_printf(" getBestPatternExample()\n");
        u_printf("\n");
        u_printf(" Use DateTimePatternGenerator to create customized date/time pattern:\n");
        u_printf(" yQQQQ,yMMMM, MMMMd, hhmm, jjmm per locale\n");
        u_printf("========================================================================\n");
		//! [getBestPatternExample]
	UnicodeString skeletons [] = {
		UnicodeString("yQQQQ"), // year + full name of quarter, i.e., 4th quarter 1999
        UnicodeString("yMMMM"), // year + full name of month, i.e., October 1999
        UnicodeString("MMMMd"), // full name of month + day of the month, i.e., October 25
        UnicodeString("hhmm"),  // 12-hour-cycle format, i.e., 1:32 PM
        UnicodeString("jjmm"), // preferred hour format for the given locale, i.e., 24-hour-cycle format for fr_FR
		0,
	};

	Locale locales[] = {
		Locale ("en_US"),
		Locale ("fr_FR"),
		Locale ("zh_CN"),
	};
	
	const char* filename = "sample.txt";
	/* open a UTF-8 file for writing */
	UFILE* f = u_fopen(filename, "w", NULL,"UTF-8");
	UnicodeString dateReturned;
	UErrorCode status =U_ZERO_ERROR;
	Calendar *cal = Calendar::createInstance(status);
	cal->set (1999,9,13,23,58,59);
	UDate date = cal->getTime(status);
	u_fprintf(f, "%-20S%-20S%-20S%-20S\n", UnicodeString("Skeleton").getTerminatedBuffer(),UnicodeString("en_US").getTerminatedBuffer(),UnicodeString("fr_FR").getTerminatedBuffer(),UnicodeString("zh_CN").getTerminatedBuffer());
	for (int i=0;skeletons[i]!=NULL;i++) {
		u_fprintf(f, "%-20S",skeletons[i].getTerminatedBuffer());
		for (int j=0;j<sizeof(locales)/sizeof(locales[0]);j++) {
			// create a DateTimePatternGenerator instance for given locale
			DateTimePatternGenerator *dtfg= DateTimePatternGenerator::createInstance(locales[j],status);
			// use getBestPattern method to get the best pattern for the given skeleton
			UnicodeString pattern = dtfg->getBestPattern(skeletons[i],status);
			// Constructs a SimpleDateFormat with the best pattern generated above and the given locale
			SimpleDateFormat *sdf = new SimpleDateFormat(pattern,locales[j],status);
			dateReturned.remove();
			// Get the format of the given date
			sdf->format(date,dateReturned,status);
			/* write Unicode string to file */
			u_fprintf(f, "%-20S", dateReturned.getTerminatedBuffer());
			delete dtfg;
			delete sdf;
		} 
		u_fprintf(f,"\n");
	}
	/* close the file resource */
	u_fclose(f);
	delete cal;
	//! [getBestPatternExample]
}
开发者ID:CODECOMMUNITY,项目名称:rust-icu,代码行数:57,代码来源:dtptngsample.cpp

示例7: slog2f

std::string GlobalizationNDK::getDatePattern(const std::string& args)
{
    DateFormat::EStyle dstyle = DateFormat::kShort, tstyle = DateFormat::kShort;

    if (!args.empty()) {
        Json::Reader reader;
        Json::Value root;
        bool parse = reader.parse(args, root);

        if (!parse) {
            slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: invalid json data: %s",
                    args.c_str());
            return errorInJson(PARSING_ERROR, "Parameters not valid json format!");
        }

        Json::Value options = root["options"];

        std::string error;
        if (!handleDateOptions(options, dstyle, tstyle, error))
            return errorInJson(PARSING_ERROR, error);
    }

    UErrorCode status = U_ZERO_ERROR;
    const Locale& loc = Locale::getDefault();
    DateFormat* df = DateFormat::createDateTimeInstance(dstyle, tstyle, loc);

    if (!df) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: unable to create DateFormat instance!");
        return errorInJson(UNKNOWN_ERROR, "Unable to create DateFormat instance!");
    }
    std::auto_ptr<DateFormat> deleter(df);

    if (df->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
        slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDatePattern: DateFormat instance not SimpleDateFormat!");
        return errorInJson(UNKNOWN_ERROR, "DateFormat instance not SimpleDateFormat!");
    }

    SimpleDateFormat* sdf = (SimpleDateFormat*) df;

    UnicodeString pt;
    sdf->toPattern(pt);
    std::string ptUtf8;
    pt.toUTF8String(ptUtf8);

    const TimeZone& tz = sdf->getTimeZone();

    UnicodeString tzName;
    tz.getDisplayName(tzName);
    std::string tzUtf8;
    tzName.toUTF8String(tzUtf8);

    int utc_offset = tz.getRawOffset() / 1000; // UTC_OFFSET in seconds.
    int dst_offset = tz.getDSTSavings() / 1000; // DST_OFFSET in seconds;

    return resultInJson(ptUtf8, tzUtf8, utc_offset, dst_offset);
}
开发者ID:ElNinjaGaiden,项目名称:GoogleMapsCordovaTest,代码行数:56,代码来源:globalization_ndk.cpp

示例8: 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

示例9: DateFormat

RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle,
                                        const Locale& locale, UErrorCode& status) :
 DateFormat(), fDateTimeFormatter(NULL), fDatePattern(), fTimePattern(), fCombinedFormat(NULL),
 fDateStyle(dateStyle), fLocale(locale), fDatesLen(0), fDates(NULL),
 fCombinedHasDateAtStart(FALSE), fCapitalizationInfoSet(FALSE),
 fCapitalizationOfRelativeUnitsForUIListMenu(FALSE), fCapitalizationOfRelativeUnitsForStandAlone(FALSE),
 fCapitalizationBrkIter(NULL)
{
    if(U_FAILURE(status) ) {
        return;
    }

    if (timeStyle < UDAT_NONE || timeStyle > UDAT_SHORT) {
        // don't support other time styles (e.g. relative styles), for now
        status = U_ILLEGAL_ARGUMENT_ERROR;
        return;
    }
    UDateFormatStyle baseDateStyle = (dateStyle > UDAT_SHORT)? (UDateFormatStyle)(dateStyle & ~UDAT_RELATIVE): dateStyle;
    DateFormat * df;
    // Get fDateTimeFormatter from either date or time style (does not matter, we will override the pattern).
    // We do need to get separate patterns for the date & time styles.
    if (baseDateStyle != UDAT_NONE) {
        df = createDateInstance((EStyle)baseDateStyle, locale);
        fDateTimeFormatter=dynamic_cast<SimpleDateFormat *>(df);
        if (fDateTimeFormatter == NULL) {
            status = U_UNSUPPORTED_ERROR;
             return;
        }
        fDateTimeFormatter->toPattern(fDatePattern);
        if (timeStyle != UDAT_NONE) {
            df = createTimeInstance((EStyle)timeStyle, locale);
            SimpleDateFormat *sdf = dynamic_cast<SimpleDateFormat *>(df);
            if (sdf != NULL) {
                sdf->toPattern(fTimePattern);
                delete sdf;
            }
        }
    } else {
        // does not matter whether timeStyle is UDAT_NONE, we need something for fDateTimeFormatter
        df = createTimeInstance((EStyle)timeStyle, locale);
        fDateTimeFormatter=dynamic_cast<SimpleDateFormat *>(df);
        if (fDateTimeFormatter == NULL) {
            status = U_UNSUPPORTED_ERROR;
            delete df;
            return;
        }
        fDateTimeFormatter->toPattern(fTimePattern);
    }

    // Initialize the parent fCalendar, so that parse() works correctly.
    initializeCalendar(NULL, locale, status);
    loadDates(status);
}
开发者ID:DavidCai1993,项目名称:node,代码行数:53,代码来源:reldtfmt.cpp

示例10: logln

/**
 * @bug 4060212
 */
void DateFormatRegressionTest::Test4060212(void) 
{
    UnicodeString dateString = "1995-040.05:01:29";

    logln( "dateString= " + dateString );
    logln("Using yyyy-DDD.hh:mm:ss");
    UErrorCode status = U_ZERO_ERROR;
    SimpleDateFormat *formatter = new SimpleDateFormat(UnicodeString("yyyy-DDD.hh:mm:ss"), status);
    if (failure(status, "new SimpleDateFormat", TRUE)) return;
    ParsePosition pos(0);
    UDate myDate = formatter->parse( dateString, pos );
    UnicodeString myString;
    DateFormat *fmt = DateFormat::createDateTimeInstance( DateFormat::FULL,
                                                            DateFormat::LONG);
    if (fmt == NULL) {
        dataerrln("Error calling DateFormat::createDateTimeInstance");
        delete formatter;
        return;
    }

    myString = fmt->format( myDate, myString);
    logln( myString );

    Calendar *cal = new GregorianCalendar(status);
    failure(status, "new GregorianCalendar");
    cal->setTime(myDate, status);
    failure(status, "cal->setTime");
    if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
        errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
                            " Want 40");

    // this is an odd usage of "ddd" and it doesn't
    // work now that date values are range checked per #3579.
    logln("Using yyyy-ddd.hh:mm:ss");
    delete formatter;
    formatter = NULL;
    formatter = new SimpleDateFormat(UnicodeString("yyyy-ddd.hh:mm:ss"), status);
    if(failure(status, "new SimpleDateFormat")) return;
    pos.setIndex(0);
    myDate = formatter->parse( dateString, pos );
    myString = fmt->format( myDate, myString );
    logln( myString );
    cal->setTime(myDate, status);
    failure(status, "cal->setTime");
    if ((cal->get(UCAL_DAY_OF_YEAR, status) != 40) || failure(status, "cal->get"))
        errln((UnicodeString) "Fail: Got " + cal->get(UCAL_DAY_OF_YEAR, status) +
                            " Want 40");

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

示例11:

UnicodeString&
RelativeDateFormat::toPatternTime(UnicodeString& result, UErrorCode& status) const
{
    if (!U_FAILURE(status)) {
        result.remove();
        if ( fTimeFormat ) {
            SimpleDateFormat* sdtfmt = dynamic_cast<SimpleDateFormat*>(fTimeFormat);
            if (sdtfmt != NULL) {
                sdtfmt->toPattern(result);
            } else {
                status = U_UNSUPPORTED_ERROR;
            }
        }
    }
    return result;
}
开发者ID:0x4d52,项目名称:JavaScriptCore-X,代码行数:16,代码来源:reldtfmt.cpp

示例12: InvokeCallbackForDatePattern

/*
Function:
InvokeCallbackForDatePattern

Gets the ICU date pattern for the specified locale and EStyle and invokes the callback with the result.
*/
bool InvokeCallbackForDatePattern(Locale& locale, DateFormat::EStyle style, EnumCalendarInfoCallback callback, const void* context)
{
	LocalPointer<DateFormat> dateFormat(DateFormat::createDateInstance(style, locale));
	if (dateFormat.isNull())
		return false;

	// cast to SimpleDateFormat so we can call toPattern()  
	SimpleDateFormat* sdf = dynamic_cast<SimpleDateFormat*>(dateFormat.getAlias());
	if (sdf == NULL)
		return false;

	UnicodeString pattern;
	sdf->toPattern(pattern);

	callback(pattern.getTerminatedBuffer(), context);
	return true;
}
开发者ID:krixalis,项目名称:coreclr,代码行数:23,代码来源:calendarData.cpp

示例13: stringConverter

bool
BCountry::TimeFormat(BString& format, bool longFormat) const
{
    icu_4_2::DateFormat* dateFormatter;
    dateFormatter = longFormat ? fICULongTimeFormatter : fICUShortTimeFormatter;
    SimpleDateFormat* dateFormatterImpl
        = static_cast<SimpleDateFormat*>(dateFormatter);

    UnicodeString ICUString;
    ICUString = dateFormatterImpl->toPattern(ICUString);

    BStringByteSink stringConverter(&format);

    ICUString.toUTF8(stringConverter);

    return true;
}
开发者ID:mmadia,项目名称:haiku-1,代码行数:17,代码来源:Country.cpp

示例14: fInfo

DateIntervalFormat::DateIntervalFormat(const Locale& locale,
                                       DateIntervalInfo* dtItvInfo,
                                       const UnicodeString* skeleton,
                                       UErrorCode& status) 
:   fInfo(NULL),
    fDateFormat(NULL),
    fFromCalendar(NULL),
    fToCalendar(NULL),
    fDtpng(NULL)
{
    if ( U_FAILURE(status) ) {
        delete dtItvInfo;
        return;
    }
    fDtpng = DateTimePatternGenerator::createInstance(locale, status);
    SimpleDateFormat* dtfmt = createSDFPatternInstance(*skeleton, locale, 
                                                    fDtpng, status);
    if ( U_FAILURE(status) ) {
        delete dtItvInfo;
        delete fDtpng;
        delete dtfmt;
        return;
    }
    if ( dtfmt == NULL || dtItvInfo == NULL || fDtpng == NULL ) {
        status = U_MEMORY_ALLOCATION_ERROR;
        // safe to delete NULL
        delete dtfmt;
        delete dtItvInfo;
        delete fDtpng;
        return;
    }
    if ( skeleton ) {
        fSkeleton = *skeleton;
    }
    fInfo = dtItvInfo;
    fDateFormat = dtfmt;
    if ( dtfmt->getCalendar() ) {
        fFromCalendar = dtfmt->getCalendar()->clone();
        fToCalendar = dtfmt->getCalendar()->clone();
    } else {
        fFromCalendar = NULL;
        fToCalendar = NULL;
    }
    initializePattern(status);
}
开发者ID:flwh,项目名称:Alcatel_OT_985_kernel,代码行数:45,代码来源:dtitvfmt.cpp

示例15: stringConverter

bool
BCountry::TimeFormat(BString& format, bool longFormat) const
{
	icu_4_2::DateFormat* dateFormatter;
 	dateFormatter = DateFormat::createTimeInstance(
		longFormat ? DateFormat::FULL : DateFormat::SHORT,
		*fICULocale);
	SimpleDateFormat* dateFormatterImpl
		= static_cast<SimpleDateFormat*>(dateFormatter);

	UnicodeString ICUString;
	ICUString = dateFormatterImpl->toPattern(ICUString);

	BStringByteSink stringConverter(&format);

	ICUString.toUTF8(stringConverter);

	return true;
}
开发者ID:mmanley,项目名称:Antares,代码行数:19,代码来源:Country.cpp


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