NSCalendar dateWithComponents提供错误的日期

时间:2011-04-05 13:51:52

标签: objective-c cocoa-touch ios4 nscalendar

我必须用dateComponents详细说明日期:

int year;
int month;
int day;
int hour;
int minute;
int second;
NSCalendar *calendar;
NSDate *actualDate;
NSDate *itemDate;
NSDateComponents *dateComps;
NSDateFormatter *dateFormatter;
UILocalNotification *sveglia;

actualDate = [NSDate date];
calendar = [[NSCalendar currentCalendar] copy];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[calendar timeZone]];
[dateFormatter setDateFormat:@"yyyy"];
year = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"MM"];
month = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"dd"];
day = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"HH"];
hour = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"mm"];
minute = [[dateFormatter stringFromDate:actualDate] intValue];
[dateFormatter setDateFormat:@"ss"];
second = [[dateFormatter stringFromDate:actualDate] intValue];
dateComps = [[NSDateComponents alloc] init];
[dateComps setTimeZone:[calendar timeZone]];
[dateComps setDay:day];
[dateComps setMonth:month];
[dateComps setYear:year];
[dateComps setHour:hour];
[dateComps setMinute:minute];
// Obtaining date from components.
itemDate = [calendar dateFromComponents:dateComps];
NSLog(@"ItemDate: %@", itemDate);

日历具有正确的TimeZone,日期的所有组件都是权限。好, 无论我做什么,itemDate对象都有Greenwich TimeZone(+0000)。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

解决了......从日期正确的那一刻起,问题就不存在了,NSDate描述总是给出GMT时间的输出。 要查看正确的格式,您应该使用descriptionWithLocale,请查看此文档。

Source