获取当前时间字符串

- (NSString *)getCurrentDate

{

    NSDateFormatter *formatter =[[[NSDateFormatteralloc] init] autorelease];  

    NSDate *date = [NSDate date];  

    [formatter setTimeStyle:NSDateFormatterMediumStyle];  

    NSCalendar *calendar = [[[NSCalendaralloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];  

    NSDateComponents *comps = [[[NSDateComponentsalloc] init] autorelease];  

    [comps setTimeZone:[NSTimeZonetimeZoneWithName:@"Asia/Shanghai"]];

    NSInteger unitFlags = NSYearCalendarUnitNSMonthCalendarUnitNSDayCalendarUnitNSWeekdayCalendarUnit | NSHourCalendarUnitNSMinuteCalendarUnitNSSecondCalendarUnit;  

 

    comps = [calendar components:unitFlags fromDate:date];  

    

    int year=[comps year];  

    NSString *month = ([comps month] < 10)?[NSStringstringWithFormat:@"0%d",[comps month]]:[NSStringstringWithFormat:@"%d", [comps month]];

    NSString *day = ([comps day] < 10)?[NSStringstringWithFormat:@"0%d",[comps day]]:[NSStringstringWithFormat:@"%d", [comps day]];

    NSString *hour = ([comps hour] < 10)?[NSStringstringWithFormat:@"0%d",[comps hour]]:[NSStringstringWithFormat:@"%d", [comps hour]];

    NSString *min = ([comps minute] < 10)?[NSStringstringWithFormat:@"0%d",[comps minute]]:[NSStringstringWithFormat:@"%d", [comps minute]];

    NSString *sec = ([comps second] < 10)?[NSStringstringWithFormat:@"0%d",[comps second]]:[NSStringstringWithFormat:@"%d", [comps second]];

        

    NSString *curDate = [[[NSString alloc] initWithFormat:@"%d%@%@%@%@%@", year, month, day, hour, min, sec] autorelease];

    

    return curDate;

}

posted @ 2012-03-13 17:15  Kalou  阅读(874)  评论(0编辑  收藏  举报