2012-03-02 14 views
1

最新のSDKとXCode 4.2でiOs 4アプリケーションを開発しています。NSDateFormatterの異常な動作

私はJSON Webサービスを使用してデータを取得しています。これらのデータの1つは日付です。 SBJsonによって解析され、その日付を変換するには

、私は、次のフォーマッタを使用します。

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"yyyy-MM-dd"]; 

を私は非同期接続を使用していて、それがWebサービスからのすべてのデータを取得するときに私は自分自身を作成するには、このコードを使用しますオブジェクト:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // do something with the data 
    // receivedData is declared as a method instance elsewhere 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    NSString *json_string = [[NSString alloc] initWithData:receivedData 
                encoding:NSUTF8StringEncoding]; 
    NSDictionary* datos = [parser objectWithString:json_string error:nil]; 

    [connection release]; 
    [receivedData release]; 
    [parser release]; 
    [json_string release]; 

    NSArray* data = [datos objectForKey:kRootKey]; 

    cierres = [[NSMutableArray alloc] initWithCapacity:data.count]; 

    NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
    [df setDateFormat:@"yyyy-MM-dd"]; 

    for (int i = 0; i < data.count; i++) 
    { 
     NSDictionary* object = [data objectAtIndex:i]; 

     NSLog(@"Parser: %@, Aper: %@, Cierre: %@", [object objectForKey:kFederacionKey], [object objectForKey:kAperturaKey], [object objectForKey:kCierreKey]); 
     NSString* fedName = [object objectForKey:kFederacionKey]; 
     NSDate* aperDate = [df dateFromString: [object objectForKey:kAperturaKey]]; 
     NSDate* cierreDate = [df dateFromString: [object objectForKey:kCierreKey]]; 

     CierreData* cierreData = 
      [[CierreData alloc] initWithFederationName:fedName 
               openDate:aperDate 
              andCloseDate:cierreDate]; 
     [cierres addObject:cierreData]; 

     [cierreData release]; 
    } 

    [delegate dataReceived]; 
} 

この文NSLog(@"Parser: %@, Aper: %@, Cierre: %@", [object objectForKey:kFederacionKey], [object objectForKey:kAperturaKey], [object objectForKey:kCierreKey]);コンソールに次の出力を示しています。

パーサ:Dinamarca、 APER:2012-01-05、Cierre:2012-01-31

しかし、aperDate変数は2012-01-04午後11時00分00秒0000です。

2012-01-05でなければならないのはなぜですか? WebブラウザでJSONデータもチェックしており、2012-01-05です。

答えて

2

ハイテクは、このコードであってもよいがお手伝いします。このようにしてください。

NSDateFormatter *formater=[[NSDateFormatter alloc]init]; 
     [formater setDateFormat:@"yyyy-MM-dd h:mm:ss a"]; 
     [formater setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]]; 

     NSDate *now1 = [formater dateFromString:[NSString stringWithFormat:@"%@ 11:07:00 PM",[[arrdate objectAtIndex:i]valueForKey:@"date"]]]; 

     NSLog(@"%@",now1); 
2

あなたはNSDateを作成するときは、このような元のために、NSDateにタイムゾーンを設定する必要がありますので、自動的にタイムゾーンが、GMT0に設定されている:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
NSTimeZone *timezone = [NSTimeZone timeZoneWithName:@"Europe/Madrid"]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd"]; 
[dateFormatter setTimeZone:timezone]; 
+0

回答ありがとうございますが、動作しません。私は同じ結果を得ています。 – VansFannel

+0

多分それは仕事をしませんでしたが、問題はタイムゾーンに関係していませんか? –

+0

私の問題は何か分かりません。 – VansFannel

関連する問題