2

i have one array of string something like below

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"

)

current formate is MM/dd/yyyy

i want to change it to dd/MM/yyyy

i am using following code for this

  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);

this is what im getting as output

  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:

my questions are why old get printed 2013-03-04 instead of 05/03/2013 at the end of first time loop it gets printed 03/05/2013 instead of 05/03/2013 and when loop is running second time it gets null value in new so thats why adding null value in array program get crashed

plz tell me what im doing wrong ?

4

4 回答 4

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循环内是否为 null,以处理错误数据。)

于 2013-03-14T06:57:55.480 回答
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];
于 2013-03-14T06:56:58.980 回答
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];
}
于 2013-03-14T07:06:12.097 回答
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];

        }
于 2013-03-14T07:00:59.463 回答