我正在尝试找出如何使用NSDateFormatter解析AD和BC日期,以及为什么0000Year解析失败。
NSString *test1 = @"0000-01-01";
NSString *test2 = @"0001-01-01";
NSString *test3 = @"0002-01-01 AD";
NSString *test4 = @"0002-01-01 BC";
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat: @"yyyy-MM-dd"];
NSLog(@"test 1 - %@", [formatter1 dateFromString: test1]);
NSLog(@"test 2 - %@", [formatter1 dateFromString: test2]);
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat: @"yyyy-MM-dd G"];
NSLog(@"test 3 - %@", [formatter2 dateFromString: test3]);
NSLog(@"test 4 - %@", [formatter2 dateFromString: test4]);这就是它返回的结果:
2012-11-26 11:24:27.536 Test[1968:c07] test 1 - (null)
2012-11-26 11:24:28.615 Test[1968:c07] test 2 - 0001-12-31 22:21:00 +0000
2012-11-26 11:24:37.604 Test[1968:c07] test 3 - 0001-12-31 22:21:00 +0000
2012-11-26 11:24:45.738 Test[1968:c07] test 4 - 0003-12-31 22:21:00 +0000如您所见,对于0000日期,它返回null,对于其他变体,它返回错误的日期。
发布于 2013-05-24 12:04:00
根据wikipedia的说法,零年在Anno Domini(AD)系统中并不存在,该系统通常用于在公历及其前身儒略历中计算年份。在这个系统中,公元前1年之后是公元1年。但是,在天文年份编号(与公元前1年重合)、ISO 8601:2004 (公元前1年与公元前1年重合)以及所有佛教和印度教历法中都有零年。
您无法正确记录日期对象。尝试使用stringFromDate函数将获取的日期转换为字符串,并记录输出。
https://stackoverflow.com/questions/13561912
复制相似问题