在我的应用中,我尝试更改正常的开始&一天结束。因此,我将当天的开始时间设置为04:00:00,并将当天结束时间设置为次日03:59:59并考虑不同的时区 我的代码如下
NSDate *date = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone localTimeZone]];
NSDateComponents *comps = [gregorian
components:NSYearCalendarUnit|NSMonthCalendarUnit|
NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|
NSTimeZoneCalendarUnit fromDate:date];
[comps setTimeZone:[NSTimeZone localTimeZone]];
// Start of Day
comps.hour = 4;
//comps.hour = 4 + ([[NSTimeZone localTimeZone] secondsFromGMT] / 60 / 60);
comps.minute = 0;
comps.second = 0;
NSDate *startOfDay = [gregorian dateFromComponents:comps];
NSLog(@"Start of day %@", startOfDay);
//Prints Out: Start of day 2014-04-16 02:00:00 +0000
// End of Day
comps.day = comps.day +1;
comps.second = -1;
NSDate *endOfDay = [gregorian dateFromComponents:comps];
NSLog(@"End of day %@", endOfDay);
//Prints Out: End of day 2014-04-17 01:59:59 +0000
拜托,我有3个问题:
1.为什么NSGregorianCalendar忽略设置其属性时区,我必须添加
时区到小时数?
2.另外,为什么不打印时区?
3.用于计算一天结束的显示代码是好的吗?