本文整理汇总了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;
}
示例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;
}
示例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;
}
示例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;
}
示例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));
}
示例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]
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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;
}
示例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;
}
示例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;
}
示例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);
}
示例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;
}