我有一个字符串数组,如下所示
newlymadeArray(
"03/05/2013",
"09/22/2013",
"03/02/2013",
"03/02/2013",
"08/22/2013",
"11/02/2013",
"07/08/2013",
"08/31/2013",
"05/13/2013",
"07/11/2013",
"10/07/2013",
"02/20/2013"
)
目前的甲酸盐是MM / dd / yyyy
我想将其更改为dd / MM / yyyy
我正在使用以下代码
NSLog(@"_newlymadeArray%@",_newlymadeArray);
//logic for changing date formate
for( int i=0; i<_newlymadeArray.count; i++){
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd/MM/yyyy"];
NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSLog(@"old %@",old);
NSString *old1 =[dateFormatter1 stringFromDate:old];
NSDate *new = [dateFormatter2 dateFromString:old1];
NSLog(@"new %@",new);
NSString *string = [dateFormatter2 stringFromDate:new];
NSLog(@"string %@",string);
[_convertedBdates addObject:string];
}
NSLog(@"_convertedBdates%@",_convertedBdates);
这就是我得到的输出
old 2013-03-04 18:30:00 +0000
new 2013-05-02 18:30:00 +0000
string 03/05/2013
old 2013-09-21 18:30:00 +0000
new (null)
string (null)
] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
我的问题是为什么旧的打印2013-03-04而不是05/03/2013 在第一次循环结束时它被打印03/05/2013而不是05/03/2013 当循环第二次运行时,它在new中获取null值,这就是为什么在数组程序中添加空值会崩溃的原因
请告诉我,我做错了什么?
答案 0 :(得分:11)
这永远不会适合:
NSString *old1 =[dateFormatter1 stringFromDate:old];
NSDate *new = [dateFormatter2 dateFromString:old1];
您说,“使用一个格式化程序格式化日期...然后使用另一个格式化程序解析它。”
您已经解析了旧格式的值。您现在需要在新的格式化程序中格式化。
我不是Objective-C开发人员,但我怀疑你只是想要:
NSDate *date = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSString *string = [dateFormatter2 stringFromDate:date];
[_convertedBdates addObject:string];
请注意,只涉及一个日期,但有两个字符串(原始格式和新格式)。
另请注意,无需在循环的每次迭代中创建一对新的格式化程序。在循环之前创建它们并重用它们:
NSDateFormatter *oldFormatter = [[NSDateFormatter alloc] init];
[oldFormatter setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *newFormatter = [[NSDateFormatter alloc] init];
[newFormatter setDateFormat:@"dd/MM/yyyy"];
for (int i = 0; i < _newlymadeArray.count; i++) {
NSDate *date = [oldFormatter dateFromString:[_newlymadeArray objectAtIndex:i]];
NSString *string = [newFormatter stringFromDate:date];
[_convertedBdates addObject:string];
}
(当然,还想要检查循环中date
是否为空,以处理错误数据。)
答案 1 :(得分:1)
试试这个:
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd/MM/yyyy"];
for( int i=0; i<_newlymadeArray.count; i++){
NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSLog(@"old %@",old);
if (old) {
NSString *newDateString = [dateFormatter2 stringFromDate:old];
NSLog(@"new %@",newDateString);
[_convertedBdates addObject:newDateString];
}
}
[dateFormatter1 release];
[dateFormatter2 release];
答案 2 :(得分:1)
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
[fromDateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *toDateFormatter = [[NSDateFormatter alloc] init];
[toDateFormatter setDateFormat:@"dd/MM/yyyy"];
for (NSString *dateString in newlyMadeArray)
{
NSDate *date = [fromDateFormatter dateFromString:dateString];
NSString *newDateString = [toDateFormatter stringFromDate:date];
[_convertedBdates addObject:newDateString];
}
答案 3 :(得分:0)
首先你应该将字符串“MM / dd / yyyy”转换为“dd / MM / yyyy”。
for( int i=0; i<_newlymadeArray.count; i++){
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"dd/MM/yyyy"];
NSDate *old = [dateFormatter1 dateFromString:[_newlymadeArray objectAtIndex:i]];
NSLog(@"old %@",old);
NSArray *arr = [[_newlymadeArray objectAtIndex:i] componentsSeparatedByString:@"/"];
NSString *old1 = [NSString stringWithFormat:@"%@/%@/%@",[arr objectAtIndex:1],[arr objectAtIndex:0],[arr objectAtIndex:2]];
NSDate *new = [dateFormatter2 dateFromString:old1];
NSLog(@"new %@",new);
NSString *string = [dateFormatter2 stringFromDate:new];
NSLog(@"string %@",string);
[_convertedBdates addObject:string];
}