刚试了一下,oc中貌似不像Java能直接获得年,月,日 。于是网上搜了下资料.
简单的获得年月日及日期的方法.
NSDate *date = [NSDate date] ;
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit fromDate:date];
NSInteger day = [components day];
NSInteger month = components.month;
NSInteger year = components.year;
NSInteger weekday = components.weekday;
NSString *weekdayString;
switch (weekday) {
case 1:
weekdayString = @"日";
break;
case 2:
weekdayString = @"一";
break;
case 3:
weekdayString = @"二";
break;
case 4:
weekdayString = @"三";
break;
case 5:
weekdayString = @"四";
break;
case 6:
weekdayString = @"五";
break;
case 7:
weekdayString = @"六";
break;
default:
weekdayString = @"?";
break;
}
NSLog(@"今天是:%zi年%zi月%zi日 星期%@",year,month,day,weekdayString);
代码比较简单.