Domanda

Come convertire una data gregorian nella data ebraico equivalente? Inoltre si prega di raccontare questi calendari come io non sto avendo molta conoscenza di questi.

È stato utile?

Soluzione

C'è una classe a portata di mano chiamato NSCalendar . Si crea uno come questo:

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

Una volta che hai gli oggetti di calendario, è possibile utilizzare per convertire una data intorno a varie rappresentazioni:

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

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

Sulla mia macchina, questo log:

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

Se si desidera convertire roba in un formato più leggibile, si utilizza 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];

Questa tronchi:

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

Sembrerebbe che NSDateFormatter sta usando la data gregoriano, ma almeno è ottenuto il nome del mese giusto, giusto?

modifica

In realtà, ho preso una cantonata. Se è sufficiente impostare il calendario del NSDateFormatter, non deve preoccuparsi di convertire la data a tutti. Vedi:

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setCalendar:hebrew];

NSLog(@"hebrew: %@", [formatter stringFromDate:[NSDate date]]);
[formatter release];

Questa tronchi:

hebrew: Shevat 4, 5771

Molto meglio! Non è Cocoa impressionante?

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top