2011-07-21 176 views
0

當我打印sourceDate的日誌時,我得到空值。 它會給NULL值。返回NULL值

的代碼是:

NSMutableString * orignalStr = [[NSMutableString alloc] init]; 
[orignalStr appendString:date]; 
[orignalStr replaceOccurrencesOfString:@"T" 
          withString:@" " 
           options:NSCaseInsensitiveSearch 
           range:NSMakeRange(0, 15)]; 
NSLog(@"The orignalString is =%@ ",orignalStr); 

NSDateFormatter* dateFormatter=[[NSDateFormatter alloc]init]; 
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz "]; 

NSDate *sourceDate =[dateFormatter dateFromString:orignalStr]; 
NSLog(@"The sourceDate is =%@ ",sourceDate); 

plz幫助我...

+0

'originalString'的日誌輸出是什麼? – 2011-07-21 07:17:03

+0

The Log應該是這樣的.. 2011-07-21 12:25:38.814 GameTimeBaseBall [1270:207] orignalString = 2011-07-19 19 GMT:10:00 2011-07-21 12:25: 38.815 GameTimeBaseBall [1270:207] dateFormatter is = 2011-07-21 12:25:38.815 GameTimeBaseBall [1270:207] sourceDate =(null) –

+0

不,我沒有得到 –

回答

1

嘗試使用此日期格式的字符串:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'",無需更換的@"T"出現次數。評論

編輯你有一個像字符串: 「2011-07-19北京時間:10:00」,格式字符串應該是@"yyyy'-'MM'-'dd' GMT:'HH':'ss"

+0

沒有它沒有工作... –

+0

對不起,認爲它會,我從[數據格式指南](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4 html的#// apple_ref/DOC/UID/TP40002369-SW1)。什麼是原始的字符串內容(你上傳的日期)? – 2011-07-21 07:29:56

+1

你應該使用'as vince提到的方式轉義靜態字符:例如:'@「yyyy' - 'MM' - 'dd''HH':'mm':'ss'zzz」'。你的格式似乎是錯誤的,你有字符串'2011-07-19 19 GMT:10:00'和格式期望類似'2011-07-19 10:00:00 zzz' –