0
후 잘못된 날짜를 반환dateFromString 나는 다음과 같은 코드가로 setLocale
NSDate *today = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; NSLocale *zh_CN = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]; [formatter setLocale:zh_CN]; [formatter setDateStyle:NSDateFormatterFullStyle]; [formatter setTimeStyle:NSDateFormatterNoStyle]; NSDateFormatter *newFormatter = [[NSDateFormatter alloc]init]; NSLocale *en_US = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"]; [newFormatter setLocale:en_US]; [newFormatter setDateStyle:NSDateFormatterFullStyle]; [newFormatter setTimeStyle:NSDateFormatterNoStyle]; NSLog(@"today: %@", today); NSString *todayString = [formatter stringFromDate:today]; NSLog(@"zh_CN todayString: %@", todayString); NSDate *backToDate = [formatter dateFromString:todayString]; NSLog(@"zh_CN backToDate: %@", backToDate); NSString *newTodayString = [newFormatter stringFromDate:today]; NSLog(@"en_US newTodayString: %@", newTodayString); NSDate *newBackToDate = [newFormatter dateFromString:newTodayString]; NSLog(@"en_US newBackToDate: %@", newBackToDate);
을 나는 backToDate = newBackToDate = 오늘 기대했지만, 로그는 다른 보여줍니다 여기에
2013-02-15 00:18:12.594 TestNSDate[16637:11303] today: 2013-02-14 16:18:12 +0000 2013-02-15 00:18:12.595 TestNSDate[16637:11303] zh_CN todayString: 2013年2月15日星期五 2013-02-15 00:18:12.596 TestNSDate[16637:11303] zh_CN backToDate: 1999-12-30 16:00:00 +0000 2013-02-15 00:18:12.619 TestNSDate[16637:11303] en_US newTodayString: Friday, February 15, 2013 2013-02-15 00:18:12.620 TestNSDate[16637:11303] en_US newBackToDate: 2013-02-14 16:00:00 +0000
우리가 backToDate를 볼 수 있습니다 1999-12-30이 된 이유가 무엇일까요?
아마도 포맷터는 제공된 날짜 문자열을 인식하지 못했습니다. –
이상 하네. 'en_US' 로케일과'newFormatter'로 제대로 작동했지만'zh_CN' 로케일과'formatter'로는 제대로 작동하지 않습니다. 주어진 날짜 포맷터가 방금 생성 한 문자열을 올바르게 구문 분석 할 것으로 기대합니다. 이게 어디서 뛰었 니? 장치 또는 시뮬레이터? iOS 버전은 무엇입니까? 다른 버전을 사용해보십시오. – rmaddy
zh_CN 로케일 포맷터에 "제한 사항"이 있다고 추측합니다. –