本文整理汇总了C++中SimpleDateFormat::format方法的典型用法代码示例。如果您正苦于以下问题:C++ SimpleDateFormat::format方法的具体用法?C++ SimpleDateFormat::format怎么用?C++ SimpleDateFormat::format使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SimpleDateFormat
的用法示例。
在下文中一共展示了SimpleDateFormat::format方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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
*************************************************************************************************/
}
示例2: pos
/**
* @bug 4029195
*/
void DateFormatRegressionTest::Test4029195(void)
{
UErrorCode status = U_ZERO_ERROR;
UDate today = Calendar::getNow();
logln((UnicodeString) "today: " + today);
SimpleDateFormat *sdf = (SimpleDateFormat*) DateFormat::createDateInstance();
if (failure(status, "SimpleDateFormat::createDateInstance")) {
return;
}
UnicodeString pat;
if(sdf == NULL){
dataerrln("Error calling DateFormat::createDateTimeInstance");
return;
}
pat = sdf->toPattern(pat);
logln("pattern: " + pat);
UnicodeString fmtd;
FieldPosition pos(FieldPosition::DONT_CARE);
fmtd = sdf->format(today, fmtd, pos);
logln("today: " + fmtd);
sdf->applyPattern("G yyyy DDD");
UnicodeString todayS;
todayS = sdf->format(today, todayS, pos);
logln("today: " + todayS);
//try {
today = sdf->parse(todayS, status);
failure(status, "sdf->parse");
logln((UnicodeString)"today date: " + today);
/*} catch(Exception e) {
logln("Error reparsing date: " + e.getMessage());
}*/
//try {
UnicodeString rt;
rt = sdf->format(sdf->parse(todayS, status), rt, pos);
failure(status, "sdf->parse");
logln("round trip: " + rt);
if(rt != todayS)
errln("Fail: Want " + todayS + " Got " + rt);
/*}
catch (ParseException e) {
errln("Fail: " + e);
e.printStackTrace();
}*/
delete sdf;
}
示例3: 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;
}
示例4: 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;
}
示例5: 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 ");
}
示例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: fp
/**
* @bug 4101483
*/
void DateFormatRegressionTest::Test4101483(void)
{
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *sdf = new SimpleDateFormat(UnicodeString("z"), Locale::getUS(), status);
if (failure(status, "new SimpleDateFormat", TRUE)) return;
FieldPosition fp(UDAT_TIMEZONE_FIELD);
//Date d = date(9234567890L);
UDate d = 9234567890.0;
//StringBuffer buf = new StringBuffer("");
UnicodeString buf;
sdf->format(d, buf, fp);
//logln(sdf.format(d, buf, fp).toString());
logln(dateToString(d) + " => " + buf);
logln("beginIndex = " + fp.getBeginIndex());
logln("endIndex = " + fp.getEndIndex());
if (fp.getBeginIndex() == fp.getEndIndex())
errln("Fail: Empty field");
delete sdf;
}
示例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
*************************************************************************************************/
}
示例9: TestJapaneseFormat
void IntlCalendarTest::TestJapaneseFormat() {
Calendar *cal;
UErrorCode status = U_ZERO_ERROR;
cal = Calendar::createInstance("ja_JP_TRADITIONAL", status);
CHECK(status, UnicodeString("Creating ja_JP_TRADITIONAL calendar"));
Calendar *cal2 = cal->clone();
delete cal;
cal = NULL;
// Test simple parse/format with adopt
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("[email protected]=japanese"), status);
SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=gregorian"), status);
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Japanese Calendar: " + str);
UnicodeString expected("September 8, 13 Heisei");
if(str != expected) {
errln("Expected " + expected + " but got " + str);
}
UDate otherDate = fmt->parse(expected, status);
if(otherDate != aDate) {
UnicodeString str3;
ParsePosition pp;
fmt->parse(expected, *cal2, pp);
fmt->format(otherDate, str3);
errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " + otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
} else {
logln("Parsed OK: " + expected);
}
delete fmt;
}
// Test parse with incomplete information
fmt = new SimpleDateFormat(UnicodeString("G y"), Locale("[email protected]=japanese"), status);
aDate = -3197117222000.0;
CHECK(status, "creating date format instance");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Japanese Calendar: " + str);
UnicodeString expected("Meiji 1");
if(str != expected) {
errln("Expected " + expected + " but got " + str);
}
UDate otherDate = fmt->parse(expected, status);
if(otherDate != aDate) {
UnicodeString str3;
ParsePosition pp;
fmt->parse(expected, *cal2, pp);
fmt->format(otherDate, str3);
errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " +
otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
} else {
logln("Parsed OK: " + expected);
}
delete fmt;
}
delete cal2;
delete fmt2;
CHECK(status, "Error occured");
// Now, try in Japanese
{
UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
UDate expectDate = 999932400000.0; // Testing a recent date
Locale loc("[email protected]=japanese");
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
}
{
UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
UDate expectDate = 999932400000.0; // Testing a recent date
Locale loc("ja_JP_TRADITIONAL"); // legacy
status = U_ZERO_ERROR;
simpleTest(loc, expect, expectDate, status);
}
{
UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
UDate expectDate = -6106032422000.0; // 1776-07-04T00:00:00Z-075258
Locale loc("[email protected]=japanese");
//.........这里部分代码省略.........
示例10: Thread
/**
* Today, the 9th day of April, is your birthday.
* So I created a page to celebrate this extraordinary day.
*/
Girl u = new Girl( "Ziqian Wang" );
// You are always elegant and beautiful
u.attr = "Grace";
// Apr. 9th, when the bell rang, your age increased
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-mm-dd hh:mm:ss" );
String dateString = formatter.format( currentTime );
if( dateString.equals( "2016-04-09 00:00:00" ) ){
u.age ++;
}
// Our blessing will be with you simultaneously.
new Thread (){
@Override
// Forever and ever. I wish
while(true){
// luckiness,
u.fortune ++;
// happiness,
u.happiness ++;
// and everything you wish can be achieved.
}.start()
// The last thing I wanna say, boring and ordinary, is:
System.out.println("Happy Birthday !");
}
示例11: getDateNames
std::string GlobalizationNDK::getDateNames(const std::string& args)
{
ENamesType type = kNamesWide;
ENamesItem item = kNamesMonths;
if (!args.empty()) {
Json::Reader reader;
Json::Value root;
bool parse = reader.parse(args, root);
if (!parse) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: 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 (!handleNamesOptions(options, type, item, error))
return errorInJson(PARSING_ERROR, error);
}
int count;
const char* pattern;
DateFormat::EStyle dstyle;
// Check ICU SimpleDateFormat document for patterns for months and days.
// http://www.icu-project.org/apiref/icu4c/classicu_1_1SimpleDateFormat.html
if (item == kNamesMonths) {
count = 12;
if (type == kNamesWide) {
dstyle = DateFormat::kLong;
pattern = "MMMM";
} else {
dstyle = DateFormat::kShort;
pattern = "MMM";
}
} else {
count = 7;
if (type == kNamesWide) {
dstyle = DateFormat::kLong;
pattern = "eeee";
} else {
dstyle = DateFormat::kShort;
pattern = "eee";
}
}
UErrorCode status = U_ZERO_ERROR;
const Locale& loc = Locale::getDefault();
DateFormat* df = DateFormat::createDateInstance(dstyle, loc);
if (!df) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: 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::getDateNames: DateFormat instance not SimpleDateFormat!");
return errorInJson(UNKNOWN_ERROR, "DateFormat instance not SimpleDateFormat!");
}
SimpleDateFormat* sdf = (SimpleDateFormat*) df;
sdf->applyLocalizedPattern(UnicodeString(pattern, -1), status);
Calendar* cal = Calendar::createInstance(status);
if (!cal) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: unable to create Calendar instance: %x.",
status);
return errorInJson(UNKNOWN_ERROR, "Unable to create Calendar instance!");
}
std::auto_ptr<Calendar> caldeleter(cal);
UCalendarDaysOfWeek ud = cal->getFirstDayOfWeek(status);
if (status != U_ZERO_ERROR && status != U_ERROR_WARNING_START) {
slog2f(0, ID_G11N, SLOG2_ERROR, "GlobalizationNDK::getDateNames: failed to getFirstDayOfWeek: %d!",
status);
return errorInJson(PARSING_ERROR, "Failed to getFirstDayOfWeek!");
}
if (ud == UCAL_SUNDAY)
cal->set(2014, 0, 5);
else
cal->set(2014, 0, 6);
std::list<std::string> utf8Names;
for (int i = 0; i < count; ++i) {
UnicodeString ucs;
sdf->format(cal->getTime(status), ucs);
if (item == kNamesMonths)
cal->add(UCAL_MONTH, 1, status);
else
cal->add(UCAL_DAY_OF_MONTH, 1, status);
if (ucs.isEmpty())
continue;
//.........这里部分代码省略.........
示例12: TestBuddhistFormat
void IntlCalendarTest::TestBuddhistFormat() {
UErrorCode status = U_ZERO_ERROR;
// Test simple parse/format with adopt
// First, a contrived english test..
UDate aDate = 999932400000.0;
SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("[email protected]=buddhist"), 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");
if(!fmt) {
errln("Coudln't create en_US instance");
} else {
UnicodeString str;
fmt2->format(aDate, str);
logln(UnicodeString() + "Test Date: " + str);
str.remove();
fmt->format(aDate, str);
logln(UnicodeString() + "as Buddhist Calendar: " + escape(str));
UnicodeString expected("September 8, 2544 BE");
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);
}
delete fmt;
}
delete fmt2;
CHECK(status, "Error occured testing Buddhist Calendar in English ");
status = U_ZERO_ERROR;
// Now, try in Thai
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
UDate expectDate = 999932400000.0;
Locale loc("th_TH_TRADITIONAL"); // legacy
simpleTest(loc, expect, expectDate, status);
}
status = U_ZERO_ERROR;
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
UDate expectDate = 999932400000.0;
Locale loc("[email protected]=buddhist");
simpleTest(loc, expect, expectDate, status);
}
status = U_ZERO_ERROR;
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
UDate expectDate = 999932400000.0;
Locale loc("[email protected]=gregorian");
simpleTest(loc, expect, expectDate, status);
}
status = U_ZERO_ERROR;
{
UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
" 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
UDate expectDate = 999932400000.0;
Locale loc("[email protected]=gregorian");
simpleTest(loc, expect, expectDate, status);
}
}
示例13: DateFormatSymbols
void
DateFormatMiscTests::test4099975()
{
/**
* Test Constructor SimpleDateFormat::SimpleDateFormat (const UnicodeString & pattern,
* const DateFormatSymbols & formatData, UErrorCode & status )
* The DateFormatSymbols object is NOT adopted; Modifying the original DateFormatSymbols
* should not change the SimpleDateFormat's behavior.
*/
UDate d = Calendar::getNow();
{
UErrorCode status = U_ZERO_ERROR;
DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
if(failure(status, "new DateFormatSymbols")) return;
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols, status);
if(failure(status, "new SimpleDateFormat")) return;
UnicodeString format0;
format0 = df->format(d, format0);
UnicodeString localizedPattern0;
localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
failure(status, "df->toLocalizedPattern");
symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
UnicodeString format1;
format1 = df->format(d, format1);
if (format0 != format1) {
errln(UnicodeString("Formats are different. format0: ") + format0
+ UnicodeString("; format1: ") + format1);
}
UnicodeString localizedPattern1;
localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
failure(status, "df->toLocalizedPattern");
if (localizedPattern0 != localizedPattern1) {
errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
+ UnicodeString("; localizedPattern1: ") + localizedPattern1);
}
delete symbols;
delete df;
}
/*
* Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols )
* Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior.
*/
{
UErrorCode status = U_ZERO_ERROR;
DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
if(failure(status, "new DateFormatSymbols")) return;
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status);
if(failure(status, "new SimpleDateFormat")) return;
df->setDateFormatSymbols(*symbols);
UnicodeString format0;
format0 = df->format(d, format0);
UnicodeString localizedPattern0;
localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
failure(status, "df->toLocalizedPattern");
symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
UnicodeString format1;
format1 = df->format(d, format1);
if (format0 != format1) {
errln(UnicodeString("Formats are different. format0: ") + format0
+ UnicodeString("; format1: ") + format1);
}
UnicodeString localizedPattern1;
localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
failure(status, "df->toLocalizedPattern");
if (localizedPattern0 != localizedPattern1) {
errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0
+ UnicodeString("; localizedPattern1: ") + localizedPattern1);
}
delete symbols;
delete df;
}
//Test the pointer version of the constructor (and the adoptDateFormatSymbols method)
{
UErrorCode status = U_ZERO_ERROR;
DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status);
if(failure(status, "new DateFormatSymbols")) return;
SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), symbols, status);
if(failure(status, "new SimpleDateFormat")) return;
UnicodeString format0;
format0 = df->format(d, format0);
UnicodeString localizedPattern0;
localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status);
failure(status, "df->toLocalizedPattern");
symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field
UnicodeString format1;
format1 = df->format(d, format1);
if (format0 != format1) {
errln(UnicodeString("Formats are different. format0: ") + format0
+ UnicodeString("; format1: ") + format1);
}
UnicodeString localizedPattern1;
localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status);
failure(status, "df->toLocalizedPattern");
if (localizedPattern0 == localizedPattern1) {
errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0
+ UnicodeString("; localizedPattern1: ") + localizedPattern1);
}
//delete symbols; the caller is no longer responsible for deleting the symbols
delete df;
//.........这里部分代码省略.........
示例14: pos
void
DateFormatMiscTests::test4097450()
{
//
// Date parse requiring 4 digit year.
//
UnicodeString dstring [] = {
UnicodeString("97"),
UnicodeString("1997"),
UnicodeString("97"),
UnicodeString("1997"),
UnicodeString("01"),
UnicodeString("2001"),
UnicodeString("01"),
UnicodeString("2001"),
UnicodeString("1"),
UnicodeString("1"),
UnicodeString("11"),
UnicodeString("11"),
UnicodeString("111"),
UnicodeString("111")
};
UnicodeString dformat [] = {
UnicodeString("yy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yyyy"),
UnicodeString("yy"),
UnicodeString("yyyy")
};
UErrorCode status = U_ZERO_ERROR;
SimpleDateFormat *formatter;
SimpleDateFormat *resultFormatter = new SimpleDateFormat((UnicodeString)"yyyy", status);
if (U_FAILURE(status)) {
dataerrln("Fail new SimpleDateFormat: %s", u_errorName(status));
return;
}
logln("Format\tSource\tResult");
logln("-------\t-------\t-------");
for (int i = 0; i < 14/*dstring.length*/; i++)
{
log(dformat[i] + "\t" + dstring[i] + "\t");
formatter = new SimpleDateFormat(dformat[i], status);
if(failure(status, "new SimpleDateFormat")) return;
//try {
UnicodeString str;
FieldPosition pos(FieldPosition::DONT_CARE);
logln(resultFormatter->format(formatter->parse(dstring[i], status), str, pos));
failure(status, "resultFormatter->format");
//if ( !dresult[i] ) System.out.print(" <-- error!");
/*}
catch (ParseException exception) {
//if ( dresult[i] ) System.out.print(" <-- error!");
System.out.print("exception --> " + exception);
}*/
delete formatter;
logln();
}
delete resultFormatter;
}
示例15: testAPI
//.........这里部分代码省略.........
UnicodeString decimalSymbol = instFromLocale->getDecimal();
UnicodeString newDecimalSymbol = UnicodeString("*");
decimalSymbol = instFromLocale->getDecimal();
instFromLocale->setDecimal(newDecimalSymbol);
DateTimePatternGenerator *cloneDTPatternGen=instFromLocale->clone();
decimalSymbol = cloneDTPatternGen->getDecimal();
if (decimalSymbol != newDecimalSymbol) {
errln("ERROR: inconsistency is found in cloned object.");
}
if ( !(*cloneDTPatternGen == *instFromLocale) ) {
errln("ERROR: inconsistency is found in cloned object.");
}
if ( *cloneDTPatternGen != *instFromLocale ) {
errln("ERROR: inconsistency is found in cloned object.");
}
delete instFromLocale;
delete cloneDTPatternGen;
// ======= Test simple use cases
logln("Testing simple use cases");
status = U_ZERO_ERROR;
Locale deLocale=Locale::getGermany();
UDate sampleDate=LocaleTest::date(99, 9, 13, 23, 58, 59);
DateTimePatternGenerator *gen = DateTimePatternGenerator::createInstance(deLocale, status);
if (U_FAILURE(status)) {
dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getGermany()) - exitting");
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);