现在的位置: 首页 > 综合 > 正文

iOS中日本历、佛历取date的问题

2013年10月08日 ⁄ 综合 ⁄ 共 492字 ⁄ 字号 评论关闭
iOS中支持的日历有三种,分别是:公历,日本公历和佛教公历。在选择日本历和佛历时输出[NSDate date]会出现错误,因为只输出了数字,而忽略了前缀(平成、昭和、明治、佛历等)的原因。为了获得正确的返回日期,可以按如下方法设置NSDateFormatter来解决:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]  init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSLocale *locale = [[NSLocale alloc]  initWithLocaleIdentifier:[[NSLocale preferredLanguages]  objectAtIndex:0]];
[dateFormatter setLocale:locale];
NSString *dateStr = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
[locale release];
输出dateStr为正常的日期字符串。


抱歉!评论已关闭.