使用 24 小时制时,小时说明符必须是大写 H,如下所示:
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
在此处检查正确的说明符:http ://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
但是,您需要为日期格式化程序设置语言环境:
// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];
完整的工作代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]];
NSDate *capturedStartDate = [dateFormatter dateFromString: @"2012-09-16 23:59:59 JST"];
NSLog(@"Captured Date %@", [capturedStartDate description]);
输出(格林威治标准时间):
Captured Date 2012-09-16 14:59:59 +0000