年、月、日のフィールドから日付を作成するのに役立つカテゴリを作成しました。また、ユリウス日 (YYJJJ) から日付を作成する必要もあります。ユリウス日文字列を解析したところ、
int years // representing the year parsed from the julian date string
int days // representing the day of the year parsed from julian date string
ここに私の NSDateCategory があります:
#import "NSDateCategory.h"
@implementation NSDate (MBDateCat)
+ (NSDate *)dateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:year];
[components setMonth:month];
[components setDay:day];
return [calendar dateFromComponents:components];
}
@end
これらのフィールドから NSDate を作成するにはどうすればよいですか?
編集: これは、Objective-C に変換しようとしている Java の機能です。
// Julian Exp Date
// (YYJJJ)
Date dt = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(dt);
int years = new Integer(mid(data, 0, 2)).intValue();
int days = new Integer(mid(data, 2, 3)).intValue();
int year = ((cal.get(Calendar.YEAR) / 20) * 20) + years;
int month = 0;
int day = 0;
cal.set(year, month, day);
cal.add(Calendar.DAY_OF_YEAR, days);
myData.setDate(cal);