Ios NSDateFormatter不适用于单个硬编码日期

Ios NSDateFormatter不适用于单个硬编码日期,ios,iphone,nsdate,nsdateformatter,nsdatecomponents,Ios,Iphone,Nsdate,Nsdateformatter,Nsdatecomponents,我使用以下代码从NSString中检索日期和年份。我正在NSLog语句中打印它们 NSDate *dateC= [[[NSDate alloc] init] autorelease]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setLocale:[NSLocale currentLocale]]; [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"]

我使用以下代码从NSString中检索日期和年份。我正在NSLog语句中打印它们

NSDate *dateC= [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"];
dateC = [dateFormat dateFromString:@"04/15/2009 00:00"];  // Only this date gives null date.
[dateFormat setDateFormat:@"MMM dd, yyyy"];
NSLog(@"date = %@",[dateFormat stringFromDate:dateC]);

NSCalendar* calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:dateC];
NSLog(@"year =%d",[components year]);
[dateFormat release];
此代码适用于除代码中提到的日期之外的所有日期。
输出应为:

日期=2009年4月15日
年份=2009年

但输出是:

日期=(空)
年份=2001年

请帮帮我为什么我会有这种奇怪的行为?

注意:我使用的是Xcode 4.6.2和iOS 6.1,请尝试这段代码。另请注意《日期格式化程序指南》中的
固定格式
下的部分,该部分在NSDateFormatter类说明中链接到

// NSDateFormatter Class Description says:
// Note that although setting a format string (setDateFormat:) in principle specifies an
// exact format, in practice it may nevertheless also be overridden by a user’s
// preferences—see Data Formatting Guide for more details.
NSLocale *locale;

NSLocale *curLocale = [NSLocale currentLocale];
NSLocale *usaLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
if([curLocale.localeIdentifier isEqualToString:usaLocale.localeIdentifier]) {
    locale = [[NSLocale alloc] initWithLocaleIdentifier:usaLocale.localeIdentifier];
    NSLog(@"Created a new one");
} else {
    NSLog(@"USed the old one");
    locale = usaLocale;
}
assert(locale);

NSDate *dateC= [[NSDate alloc] init];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:locale];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
assert(calendar);
[dateFormat setCalendar:calendar];
[dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"];

dateC = [dateFormat dateFromString:@"04/15/2009 00:00"];  // O
NSLog(@"DATE: %@", dateC);

如果显示的是不同的小时,则可能需要设置默认时区(仅在ios7之后发生)


我试过你的代码,它对硬编码日期也很有效。我得到的日志是:日期=2009年4月15日,年份=2009年。我试过了,它对我有效。问,为什么dateC的alloc/init/autorelase?我总是得到这样的结果:-[\uu NSCFCalendar components:fromDate::]:date不能为零我是说真的,你认为这个操作对于零日期意味着什么?目前已避免出现例外情况。这些错误中的一小部分将随此投诉一起报告,然后进一步的违规行为将只是默默地执行nil产生的任何随机结果。它在Xcode 5.0.1 iOS7上运行良好:NSDate*dateC=[[NSDate alloc]init];NSDateFormatter*dateFormat=[[NSDateFormatter alloc]init];[dateFormat setLocale:[NSLocale currentLocale]];[日期格式setDateFormat:@“MM/dd/yyyy HH:MM”];dateC=[dateFormat dateFromString:@“04/15/2009 00:00”];//只有此日期提供空日期。NSLog(@“日期=%@”,dateC);几乎可以肯定这是您的区域设置-尝试使用以下区域设置“[[NSLocale alloc]initWithLocaleIdentifier:@“en_US”]”和/或日历[dateFormat setCalendar:[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]]Heyyyyy Dav。。。你变魔术了。现在可以工作了。。非常感谢。经验真的很重要。@ShahidIqbal,那么您是得到了新区域设置的日志消息,还是使用了旧区域设置?这里没有魔法-我只是反复阅读文档!大卫。我用NSLog语句检查我当前的语言环境标识符,它显示了“en_US”。所以若语句被执行并显示“Createanewone”消息。但我发现,问题不在于地点。问题是,我没有将日历设置为NSDateFormatter setCalendar方法。因此,只需添加[dateFormat setCalendar:[NSCalendar AutoupdateingCurrentCalendar]];我的代码中的第行完成了这个任务。但再次感谢,我可以通过查看您的代码来做到这一点。如果您阅读文档,用户可以更改设备上的日期格式,并中断固定格式转换,从而创建新的区域设置。我不知道这是OSX的问题还是ios也会发生。在任何情况下,按照上面的方式做可能更安全。
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];