NSDate和NSString之间的转换

NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间

好像是从ios4.1开始[NSDate date];获取的是GMT时间,这个时间和北京时间相差8个小时,以下代码可以解决这个问题

//方法一

- (void)tDate

{

    NSDate *date = [NSDatedate];

    NSTimeZone *zone = [NSTimeZonesystemTimeZone];

    NSInteger interval = [zone secondsFromGMTForDate: date];

    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];  

    NSLog(@"%@", localeDate);

}

方法二

- (NSString*)dateAsString:(NSDate*)date

 {

/* Create a single string expressing a mountain's climbed date, properly localized */

NSString *returnValue = @"";

    NSDateFormatter *dateFormatter = nil;

if (date != nil) {

if (dateFormatter ==nil) {

dateFormatter = [[NSDateFormatteralloc]init];

}

//原文地址:http://blog.csdn.net/diyagoanyhacker/article/details/7096612

//作者:禚来强

[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

[dateFormatter setTimeStyle:NSDateFormatterNoStyle];

[dateFormatter setLocale:[NSLocalecurrentLocale]];

returnValue = [dateFormatter stringFromDate:date];

}

/* As this code uses the current "locale", the date format will be in the format 

     specified by the user's "Region Format" settings.  If you need to use an

     alternate format internally, you can create and use NSLocales, e.g.:

 

     NSLocale *enGBLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

     [numberFormatter setLocale:enGBLocale];

 

     Doing so will not affect the current user-set language or region format. 

     

     Similarly, while you should always rely on the system and application bundle

     to pick the most appropriate resources for the current user language setting,

     if you need to know what the current user language setting is, you can do

     something like the following:

 

     NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];

     NSArray* languages = [defs objectForKey:@"AppleLanguages"];

     NSString* preferredLang = [languages objectAtIndex:0];

     NSLog(@"Current language is %@", preferredLang);

 

     Note that the iPhone does not support locales in the same way that Mac OS

     does (really only using locales for the Region Format settings) so if you

     try and get an array from standardUserDefaults for the key "AppleLocale",

     this will fail on the iPhone. */

return returnValue;

}


//方法三

+ (NSString *)fixStringForDate:(NSDate *)date 

{

    NSDateFormatter* dateFormatter = [[NSDateFormatteralloc]init];

    [dateFormatter setDateStyle:kCFDateFormatterFullStyle];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *fixString = [dateFormatter stringFromDate:date]; 

    [dateFormatter release];

    return fixString;

}


方法四

    NSDateComponents *comps = [[NSDateComponentsalloc]init];

    [comps setYear:2010];

    [comps setMonth:8];

    [comps setDay:24];

    [comps setHour:17];

    [comps setMinute:5];

    [comps setTimeZone: [NSTimeZonetimeZoneWithAbbreviation:@"UTC"]];

    NSLog(@"%@", [NSTimeZonetimeZoneWithAbbreviation:@"UTC"]);

    

    NSCalendar *cal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *referenceTime = [cal dateFromComponents:comps];

    NSLog(@"%@", referenceTime);


你可能感兴趣的:(Date,String,user,application,System,resources)