IST time zone not getting parsed

2019-08-27 23:28发布

问题:

I have a string Tue Feb 23 10:12:48 IST 2014, I want to get the NSdate object form this. I am using below code snippet

    NSString * buildTime = @"Tue Feb 23 10:12:48 IST 2014";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
    NSDate *date = [formatter dateFromString:buildTime ];

Every time I am getting nil value to date variable. If I am changing IST to GMT in build time then NSDate is perfectly coming. Can any one correct me where I am doing mistake in this.

回答1:

Use small 'zzz' instead of 'ZZZ',

NSString * buildTime = @"Tue Feb 23 10:12:48 IST 2014";
NSLocale *indianEnglishLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_IN"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:indianEnglishLocale];
[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss zzz yyyy"];
NSDate *date = [dateFormatter dateFromString:buildTime];
NSLog(@"date %@",date);
NSLog(@"string %@",[dateFormatter stringFromDate:date]);