Objective-C:如何从儒略日期字段创建 NSDate

发布于 2024-10-09 08:38:59 字数 1484 浏览 2 评论 0原文

我创建了一个类别来帮助我处理从年、月、日字段创建日期。我现在还需要从儒略日期 (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?

编辑: 这是我试图将 Java 中的功能转换为 Objective-C 的功能

// 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);

I've created a category to help me deal with creating dates from year, month, day fields. I now also have the need to create a date from a Julian Date (YYJJJ). I've parsed my Julian date string and now have

int years  // representing the year parsed from the julian date string
int days  // representing the day of the year parsed from julian date string

here is my 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

How do I create an NSDate from these fields?

Edit:
Here is the functionality in Java that I am trying to translate into Objective-C

// 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);

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

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

发布评论

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

评论(1

我纯我任性 2024-10-16 08:38:59

使用NSDateFormatter,给定正确的格式字符串,它可以实现您想要的:

NSString *dateString = @"10123";      // 123. day of 2010
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyDDD"];
NSDate *aDate = [fmt dateFromString:dateString];
[fmt release];
NSLog(@"Date: %@", aDate);

这将返回:

Date: 2010-05-03 00:00:00 +0200

Use NSDateFormatter, given the right format string it achieves what you want:

NSString *dateString = @"10123";      // 123. day of 2010
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"yyDDD"];
NSDate *aDate = [fmt dateFromString:dateString];
[fmt release];
NSLog(@"Date: %@", aDate);

This returns:

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