- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我相信我可能在 NSDateFormatter
中发现了一个错误,我不确定这是否本地化为在系统偏好设置中指定了巴西时区。
完整项目在这里:http://www.idanfe.com/dl/nsDateFormatterPuzzle.zip
相关代码摘录如下:
NSArray *unformattedDatesArray = [[NSArray alloc] initWithObjects:@"2011-09-18",
@"2011-10-16",@"2011-11-13",@"2011-12-11",@"2012-01-08",@"2012-02-05", nil];
NSString *resultedString = @"";
int ii = 0;
NSUInteger items = [unformattedDatesArray count];
for (ii=0; ii<items; ii++) {
NSLog(@"original item # %d = %@",ii,[unformattedDatesArray objectAtIndex:ii]);
resultedString = [resultedString stringByAppendingString:@"original item # "];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat: @"%d = ",ii]];
resultedString = [resultedString stringByAppendingString:[unformattedDatesArray objectAtIndex:ii]];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat:@"\n"]];
}
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
NSString *formattedDate;
for (ii=0; ii<items; ii++) {
formattedDate = [newFormat stringFromDate:[originalFormat
dateFromString:[unformattedDatesArray objectAtIndex:ii]]];
NSLog(@"formatted item # %d = %@",ii,formattedDate);
resultedString = [resultedString stringByAppendingString:@"formatted item # "];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat: @"%d = ",ii]];
resultedString = [resultedString stringByAppendingString:(formattedDate == nil ? @"ERROR" : formattedDate)];
resultedString = [resultedString stringByAppendingString:[NSString stringWithFormat:@"\n"]];
}
[resultingStringTextView insertText:resultedString];
基本上,我从一个包含 5 个代表日期的 NSString
的数组开始。我正在创建 2 个 NSDateFormatter
:originalFormat
用于将日期的原始字符串表示形式转换为实际的 NSDate
; newFormat
用于创建使用 originalFormat
日期格式化程序生成的 NSDate
的新所需字符串表示形式。
这是我的结果日志:
2012-08-26 nsDateFormatterPuzzle[3261:303] original item # 0 = 2011-09-18
2012-08-26 nsDateFormatterPuzzle[3261:303] original item # 1 = 2011-10-16
2012-08-26 nsDateFormatterPuzzle[3261:303] original item # 2 = 2011-11-13
2012-08-26 nsDateFormatterPuzzle[3261:303] original item # 3 = 2011-12-11
2012-08-26 nsDateFormatterPuzzle[3261:303] original item # 4 = 2012-01-08
2012-08-26 nsDateFormatterPuzzle[3261:303] original item # 5 = 2012-02-05
2012-08-26 nsDateFormatterPuzzle[3261:303] formatted item # 0 = 18/09/2011
2012-08-26 nsDateFormatterPuzzle[3261:303] formatted item # 1 = (null)
2012-08-26 nsDateFormatterPuzzle[3261:303] formatted item # 2 = 13/11/2011
2012-08-26 nsDateFormatterPuzzle[3261:303] formatted item # 3 = 11/12/2011
2012-08-26 nsDateFormatterPuzzle[3261:303] formatted item # 4 = 08/01/2012
2012-08-26 nsDateFormatterPuzzle[3261:303] formatted item # 5 = 05/02/2012
请注意,如果您转到系统偏好设置 --> 日期和时间 --> 时区
并将您的时区更改为里约热内卢,您将获得相同的结果。
================================ 尝试解决方法 ============ =======================
我正在尝试解决此错误,而 Apple 并未修复它。
我有两个问题,一个是从这个日期开始渲染 PDF 文件,我想我已经通过以下方式解决了这个问题:
if (formated_issue_date == NULL) {
formated_issue_date = [BHDateFormatter bhDateFormat:data_emissao];
}
使用这个处理程序:
+(NSString*)bhDateFormat:(NSString *)inputedDate{
NSString *formattedDate = @"";
NSRange range82 = NSMakeRange (8, 2);
NSRange range52 = NSMakeRange (5, 2);
NSRange range04 = NSMakeRange (0, 4);
NSString *firstBlock = [inputedDate substringWithRange:range82];
NSString *secondBlock = [inputedDate substringWithRange:range52];
NSString *thirdBlock = [inputedDate substringWithRange:range04];
formattedDate = [formattedDate stringByAppendingString:firstBlock];
formattedDate = [formattedDate stringByAppendingString:@"/"];
formattedDate = [formattedDate stringByAppendingString:secondBlock];
formattedDate = [formattedDate stringByAppendingString:@"/"];
formattedDate = [formattedDate stringByAppendingString:thirdBlock];
return formattedDate;
}
第二个问题是发票日期被加载到一个包含日期格式化程序的 TableView 中,更改它会导致表的排序丢失。
使用与我的处理程序相同的原则,并坚持传递 16/10/2011 日期会导致 TableView 出现错误,例如:-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x40265f3a0
这是迄今为止我找到的最好的代码:
if (formatterDate == NULL) {
NSLog (@"outprintString '%@' = NULL", dateString);
NSString *newDate = [BHDateFormatter bhDateFormat:dateString];
if ([newDate isEqualToString:@"16/10/2011"]) {
formatterDate = [inputFormatter dateFromString:@"2011-10-17"];
NSLog(@"formatterDate = %@", formatterDate);
NSAlert *alert = [[[NSAlert alloc]init] autorelease];
[alert setMessageText:[NSString stringWithFormat:NSLocalizedString(@"Mountain Lion Bug: I have found a date \"16/10/2011\" in your invoice # %@ issued by %@, I must rename the date in your table view to \"17/10/2011\", the date on the rendered invoice file will be display correctly. I apologize for this, but it is an operating system level bug that has to be fixed by Apple.","A comment here"),invoiceNumber, emitente]];
[alert runModal];
}
}
你能评论一下吗?
谢谢!
最佳答案
我相信你是对的——这是一个错误,你应该报告它。有趣的是,如果您将时区设置为与里约热内卢处于同一时区的努克格陵兰,它可以正常工作。此外,“2011-10-15”和“2011-10-17”适用于里约时区,但不适用于“2011-10-16”,这真的很奇怪。
这是我用来测试的简化代码:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSArray *unformattedDatesArray = [[NSArray alloc] initWithObjects:@"2011-09-18",@"2011-10-16",@"2011-11-13",@"2011-12-11",@"2012-01-08",@"2012-02-05", nil];
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
for (NSString * aString in unformattedDatesArray) {
NSString *formattedDate = [newFormat stringFromDate:[originalFormat dateFromString:aString]];
NSLog(@"Original date string is: %@ formatted date as string is: %@",aString, formattedDate);
}
}
补充信息:
为了找到这个问题的根源,我使用以下代码遍历了 2011 年的所有日期:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSDateFormatter *originalFormat = [[NSDateFormatter alloc] init];
[originalFormat setDateFormat:@"yyyy-MM-dd"]; // hh:mm:ss
NSDateFormatter *newFormat = [[NSDateFormatter alloc] init];
[newFormat setDateFormat:@"dd/MM/yyyy"]; // hh:mm:ss
NSDate *start = [originalFormat dateFromString:@"2011-01-01"];
NSDate *newDate;
for (int i=0; i<365; i++) {
newDate = [start dateByAddingTimeInterval:86400 * i];
NSString *formattedDate = [newFormat stringFromDate:newDate];
NSLog(@"formatted date as string is: %@",formattedDate);
}
}
当您查看日期字符串时,您会发现有 2 个 2 月 19 日,而没有 10 月 16 日,这就是为什么该日期为空(尽管如果您记录日期对象本身,您会正确获取所有日期) .
关于objective-c - NSDateFormatter 难题!这是新的 Mountain Lion Bug 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12167829/
我在 iOS 6 上使用 xcode 4.5(4G182)。NSDateFormatter 在 iOS 6 中显示错误的年份,如何解决? NSDateFormatter *dateFormatter
我在 iOS 6 上使用 xcode 4.5(4G182)。NSDateFormatter 在 iOS 6 中显示错误的年份,如何解决? NSDateFormatter *dateFormatter
我正在尝试创建一个 TableViewCell,就像 iPhone 上的 Notes 应用程序中的那样。标题在左侧,时间在右侧,紧挨着披露者指示符。 看起来像那样的 tableView。我知道我必须使
我有一个用于“从”和“到”字段的日期选择器,我想要减法的结果。例如:toValue-fromValue,结果以小时为单位。如何做到这一点? 最佳答案 可以使用timeIntervalSinceDate
我简单地尝试重新格式化日期: NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSDate *date = [[NSDat
在 Instrument 的帮助下,我发现以下代码段存在内存泄漏。仪器显示 NSDateFormatter 此处泄漏内存。 - (NSDate*) dateSelected{ if(selectedD
当用户未使用公历作为 iPhone 的默认日历时,我在使用 NSDateFormatter 时遇到问题。 NSString *testString = @"2011-01-14"; NSDateFor
以下日期/时间的 NSDateFormatter 字符串是什么:02/22/2011 10:43:00 AM 基本上我想要的是读取字符串02/22/2011 19:00:23 PM,将其转换为NSDa
我正在尝试创建一个格式化程序,将显示的日期格式转换为 NSDate 对象: NSString *dateStr = @"2010-06-21T19:00:00-05:00"; NSDateFormat
我有一个带有 NSDateFormatter 的 NSTextField。格式化程序接受“mm/dd/yy”。 可以自动补全日期吗?因此,用户可以输入“mm”,格式化程序将完成当前月份和年份。 最佳答
我正在从表示日期的文本中提取字符串。它们看起来像这样: Monday August 16, 2010 05:28 AM EST 我正在尝试使用 NSDateFormatter 来解析它们。我已将其格式
我有一个应用于 NSTextFieldCell 的 10.4 样式日期格式化程序。我调用 setTwoDigitStartDate: (尝试使用纪元日期和“引用”日期),但是当我输入带有两位数年份的日
在打开 doesRelativeDateFormatting 的情况下使用 NSDateFormatter 时,所有相对格式似乎都相对于当前系统时间。我想使用相对格式设置日期,例如“今天”或“明天”,
我想从日期获取 NSString,日期格式如下: MMEEEY ww 问题出在“ww”,即一年中的第几周。我必须将此格式与从后台获取的另一种格式进行比较。但后台的第一个工作日是星期一,而 iOS 的第
我的日期格式化程序返回nil表示一个特定日期:“1998年3月20日”。 + (NSDate*)dateFromString:(NSString*)date { NSDateFormatter
我知道这篇文章可能一式三份,但是到目前为止,关于现有线程的所有答案都没有帮助我。 我试图从字符串中获取NSDate值,但结果一直是nil。这是我的代码 NSString * dateString =
Crashlytics服务报告一些崩溃(大约30个崩溃/ 18个用户进行1000个 session ) 这是我的代码: var brutDate: String = "" brutDate () (
我正在尝试解析这个日期时间戳 开始串: Wed, 11 Sep 2013 08:51:41 EEST 这个问题只有 EEST , 我试过 z 或 zzz 或 V,什么也没发生。日期格式化程序始终为 N
这个问题在这里已经有了答案: NSDate Format outputting wrong date (5 个回答) 8年前关闭。 我正在使用下面的代码,但它给了我提前一天 date 。 示例 :将输
使用设备上用户的currentLocale,我想要一种与NSDateFormatterFullStyle生成的日期格式完全相同的日期格式减去年份: [dateFormatter setDateStyl
我是一名优秀的程序员,十分优秀!