公历到希伯来语

发布于 2024-10-11 08:07:05 字数 48 浏览 2 评论 0原文

如何将公历日期转换为等效的希伯来日期? 另外请告诉我这些日历,因为我对此了解不多。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-18 08:07:05

有一个方便的类,名为 <代码>NSCalendar。您可以这样创建一个:

NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendar * hebrew = [[NSCalendar alloc] initWithCalendarIdentifier:NSHebrewCalendar];

一旦获得了日历对象,您就可以使用它们将日期转换为各种表示形式:

NSDate * date = [NSDate date];
NSDateComponents * components = [gregorian components:NSUIntegerMax fromDate:date];
NSDate * hebrewDate = [hebrew dateFromComponents:components];

NSLog(@"date: %@", date);
NSLog(@"hebrew: %@", hebrewDate);

在我的机器上,此日志:

date: 2011-01-09 23:20:39 -0800
hebrew: 1751-09-25 23:20:39 -0800

如果您想将内容转换为更易读的格式,您可以使用 < a href="http://developer.apple.com/library/ios/#documentation/cocoa/reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html" rel="noreferrer">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 正在使用公历日期,但至少它有正确的月份名称,对吗?

编辑

实际上,我搞砸了。如果您只是设置NSDateFormatter的日历,则根本不必担心日期转换。请参阅:

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

好多了!可可不是很棒吗?

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

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];

This logs:

date: January 9, 2011
hebrew: Tishri 9, 2011

It would appear that NSDateFormatter is using the gregorian date, but at least it's got the right month name, right?

edit

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];

This logs:

hebrew: Shevat 4, 5771

Much better! Isn't Cocoa awesome?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文