[iphone-cocoa]使用NSDate得到,某天之后的一天

// start by retrieving day, weekday, month and year components for yourDate
   
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:
NSGregorianCalendar];
   
NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | 
NSMonthCalendarUnit | NSYearCalendarUnit) yourDate];
   
NSInteger theDay = [todayComponents day];
   
NSInteger theMonth = [todayComponents month];
   
NSInteger theYear = [todayComponents year];

   
// now build a NSDate object for yourDate using these components
   
NSDateComponents *components = [[NSDateComponents alloc] init];
   
[components setDay:theDay];
   
[components setMonth:theMonth];
   
[components setYear:theYear];
   
NSDate *thisDate = [gregorian dateFromComponents:components];
   
[components release];

   
// now build a NSDate object for the next day
   
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
   
[offsetComponents setDay:1];
   
NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents 
toDate: yourDate options:0];
   
[offsetComponents release];
   
[gregorian release];
posted @ 2010-05-21 16:39  AlexLiu  阅读(1560)  评论(0编辑  收藏  举报