1

Google カレンダー Web サービスを介して rfc3399 形式の日付文字列を取得しています。この文字列を NSDate オブジェクトに変換して、これに関するイベントをローカル カレンダーに追加できるようにします。NSDateFormatter に関するAppleのドキュメントで以下の方法を見つけましたが、機能していません

日付文字列- 2012-05-23T18:30:00.000-05:00

 - (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
 Returns a user-visible date time string that corresponds to the specified
 RFC 3339 date time string. Note that this does not handle all possible
 RFC 3339 date time strings, just one of the most common styles.
 */

NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];

NSString *userVisibleDateTimeString;
if (date != nil) {
    // Convert the date object to a user-visible date string.
    NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
    assert(userVisibleDateFormatter != nil);

    [userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];

    userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}

return userVisibleDateTimeString;
}

助けてください。日付形式を何度も変更しようとしましたが、成功しませんでした。

4

2 に答える 2

1

この方法で試してみてください..

-(NSDate *)convertStringToDate:(NSString *) date {
        NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; 
        NSDate *nowDate = [[[NSDate alloc] init] autorelease];
        [formatter setDateFormat:@"YYYY-MM-DD'T'HH:mm:ssZ"];// set format here which format in string date
        /// also try this format @"yyyy-MM-dd'T'HH:mm:ss.fffK" 
        date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
        nowDate = [formatter dateFromString:date];
        // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", nowDate);
        return nowDate;
}

このメソッドを次のように呼び出します..

NSDate *date = [self convertStringToDate:rfc3339DateTimeString];

fffKまた、この形式については、いくつかのrfc Date形式で試してください。これは以下を参照してください。リンク..

how-do-i-parse-and-convert-datetimes-to-the-rfc-3339-date-time-format

この回答がお役に立てば幸いです

于 2012-11-20T11:39:03.900 に答える
0

問題は、タイムゾーン値に (-05:00 )':' が含まれる日付です...文字列からそれを削除する必要があり、上記の方法は正常に機能します...新しい日付文字列は次のようになります下

2012-05-23T18:30:00.000-0500 - これは正しい形式です。

于 2012-11-24T06:11:18.707 に答える