gpt4 book ai didi

ios - NSDateFormatter奇怪的行为

转载 作者:行者123 更新时间:2023-12-01 18:30:27 24 4
gpt4 key购买 nike

我正在使用最新的SDK和XCode 4.2开发iOs 4应用程序。

我正在使用JSON网络服务来获取一些数据。这些数据之一是日期。

要转换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 23:00:00 +0000

为什么我必须在2012-01-05时收到2012-01-04?我还在Web浏览器上检查了JSON数据,并且是2012年1月5日。

最佳答案

您好,这段代码可以为您提供帮助。尝试这样。

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

关于ios - NSDateFormatter奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9530746/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com