本人用setDateFormat试来试去只能实现“2003”,后面那个“年”字没有。
可以本地化语言翻译“年”字,中文显示“年”字,英文显示“”,但是这是对应于系统设置的语言,跟区域格式无关。区域格式,有办法显示“2003年”吗?
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *cnLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
[formatter setLocale:cnLocale];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"Date for locale %@: %@",
[[formatter locale] localeIdentifier], [formatter stringFromDate:[NSDate date]]);
}
return 0;
}
我突然发现,你可能更需要的是这段代码,,,不过,你都是不看 API 文档的么?
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *cnLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"];
NSString *dateFormat;
NSString *dateComponents = @"yyyy";
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:cnLocale];
[formatter setDateFormat:dateFormat];
NSLog(@"Date for locale %@: %@",
[cnLocale displayNameForKey:NSLocaleIdentifier value:@"zh"] , [formatter stringFromDate:[NSDate date]]);
}
return 0;
}