Created
October 5, 2012 13:22
-
-
Save eienf/3839779 to your computer and use it in GitHub Desktop.
Extract Japanese Era name only after Mac OS X 10.8/iOS6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSCalendar *japanese = [[NSCalendar alloc] initWithCalendarIdentifier:NSJapaneseCalendar]; | |
NSLocale *aLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; | |
NSDateFormatter *aFormatter = [[NSDateFormatter alloc] init]; | |
aFormatter.calendar = japanese; | |
aFormatter.locale = aLocale; | |
aFormatter.dateFormat = @"G"; | |
NSDateComponents *comps = [[NSDateComponents alloc] init]; | |
comps.era = 231; // 0-235 | |
comps.year = 1; | |
comps.month = 12; | |
comps.day = 31; | |
NSDate *aDate = [japanese dateFromComponents:comps]; | |
NSString *aString = [aFormatter stringFromDate:aDate]; | |
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(.*)\\b.\\(.*\\)" | |
options:0 | |
error:NULL]; | |
aString = [regex stringByReplacingMatchesInString:aString | |
options:0 | |
range:NSMakeRange(0, [aString length]) | |
withTemplate:@"$1"]; | |
NSLog(@"[%@]",aString); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment