NSCalendar dateWithComponents 给出错误的日期
我必须用 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);
日历具有正确的时区,日期的所有组件都是正确的。出色地, 无论我做什么,itemDate 对象都有格林威治时区(+0000)。我应该如何解决这个问题?
I have to elaborate a date with 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);
calendar has the correct TimeZone, all the components of the Date are rights. Well,
no matter what I do, itemDate object has the Greenwich TimeZone (+0000). How should I fix this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
已解决...自从日期正确后,问题就不存在了,NSDate 描述始终给出 GMT 时间的输出。
要查看正确的格式,您应该使用descriptionWithLocale,请查看该格式的文档。
来源
Solved... The problem did not exists since the moment that the date was correct, the NSDate description give always the output in GMT time.
To see the correct format you should use descriptionWithLocale, check the documentation for this one.
Source