格里高利到希伯来语 [英] Gregorian to Hebrew
问题描述
如何将格里高利日转换为等效的希伯来日期?
另外请告诉我们这些日历,因为我对这些日历知之甚少。
How to convert a gregorian date into the equivalent hebrew date? Also please tell about these calendars as I am not having much knowledge of these.
推荐答案
有一个方便的课程叫做 NSCalendar
。你创建一个这样的:
There's a handy class called NSCalendar
. You create one like this:
NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];
获得日历对象后,您可以使用它们将日期转换为各种表示形式:
Once you've got the calendar objects, you can use them to convert a date around to various representations:
NSDate * date = [NSDate date];
NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];
NSDate * hebrewDate = [hebrew dateFromComponents:components];
NSLog(@"date: %@", date);
NSLog(@"hebrew: %@", hebrewDate);
在我的机器上,这个日志:
On my machine, this logs:
date: 2011-01-09 23:20:39 -0800
hebrew: 1751-09-25 23:20:39 -0800
如果您想将内容转换为更易读的格式,请使用 NSDateFormatter
:
If you want to convert stuff to a more readable format, you use NSDateFormatter
:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setCalendar:gregorian]; //this is usually unnecessary; it's here for clarity
NSLog(@"date: %@", [formatter stringFromDate:date]);
[formatter setCalendar:hebrew];
NSLog(@"hebrew: %@", [formatter stringFromDate:hebrewDate]);
[formatter release];
此日志:
date: January 9, 2011
hebrew: Tishri 9, 2011
看起来 NSDateFormatter
正在使用格里高利日期,但至少它有正确的月份名称,对吗?
It would appear that NSDateFormatter
is using the gregorian date, but at least it's got the right month name, right?
编辑
实际上,我傻了。如果您只设置 NSDateFormatter
的日历,则无需担心转换日期。请参阅:
Actually, I goofed. If you just set the calendar of the NSDateFormatter
, you don't have to worry about converting the date at all. See:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setCalendar:hebrew];
NSLog(@"hebrew: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];
此日志:
hebrew: Shevat 4, 5771
好多了!可可不是很棒?
Much better! Isn't Cocoa awesome?
这篇关于格里高利到希伯来语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!