2012-04-02 91 views
2

我有一個功能,通過使用NSDateFormatter轉換爲NSDateNSDateFormatter IOS錯誤

+(NSDate*)getDateFromCurrentTimestamp:(NSString*)currentTimestamp{ 
// currentTimestamp= @"28-Feb-2012 16:42:19 PM"; 

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; 
    [dateFormatter setDoesRelativeDateFormatting:NO]; 
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
    [dateFormatter setFormatterBehavior:NSDateFormatterShortStyle]; 
    //[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault]; 
    [dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss a"]; 

    return [dateFormatter dateFromString:currentTimestamp]; 
} 

IOS 5.x返回正確NSDate。 當在4.1上運行時& 4.2也返回正確的NSDate。 當在4.3.2上運行時& 4.3.3,它返回null

怎麼可能?這個的任何解決方法?


編輯

這是造成的iOS漏洞。這與手機本身有關。有些手機在設備中設置了不同的locale。所以我們不得不迫使它使用一個locale

+1

是你的'region'和'locale'設置爲4.3.2和4.3.3的設備?看看[這個問題](http://stackoverflow.com/questions/1559182/how-do-you-interpret-dates-with-nsdateformatter/1559284#1559284)幫助 – tipycalFlow 2012-04-02 04:46:56

+0

是的,你可能是正確的。也許這個問題與'locale'不是ios版本有關。我會嘗試一下。 – HelmiB 2012-04-02 06:52:21

+0

它的工作:'[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@「en_GB」] autorelease]];'。謝謝。太糟糕了,不能接受你的回答。 – HelmiB 2012-04-02 07:24:56

回答

1

從我們的討論中的意見:

是你的區域和語言環境爲4.3.2 & 4.3.3設備設置?看看SO question是否有幫助。

相關代碼:

[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]]; 
+0

如果有人想知道,'en_GB'等於'UK'。 – HelmiB 2012-04-03 04:28:13