从佛历转换为公历日期格式时,NSDateFormatter 行为不正常 [英] NSDateFormatter not behaving properly when converting from Buddhist calendar to Gregorian calendar date formats

查看:78
本文介绍了从佛历转换为公历日期格式时,NSDateFormatter 行为不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个支持将日期从佛教日历转换为公历的应用程序(注意:我正在测试的设备的常规 > 国际 > 日历"设置是佛教".).但是,我不明白为什么 NSDateFormatter 不能正确解析我的日期.这是我的代码:

I'm making an app that supports conversion of dates from the Buddhist calendar to Gregorian (Note: The "General > International > Calendar" settings of the device I'm testing on is "Buddhist".). However, I can't figure out why the NSDateFormatter doesn't parse my dates properly. Here's my code:

NSDate *now = [NSDate date];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.calendar = gregorianCalendar;
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";

NSString *formattedDate = [formatter stringFromDate:now];
NSDate *boomDate = [formatter dateFromString:formattedDate];

NSLog(@"now: %@, formattedDate (as string) > %@, boomDate (as date) > %@", now, formattedDate, boomDate);

Xcode 的日志说:

Xcode's log says:

now: 2556-05-23 07:11:03 +0000, formattedDate (as string) > 2013-05-23 15:11:03 +0800, boomDate (as date) > 2556-05-23 07:11:03 +0000

当我将 formattedDate(它是一个 NSString)转换为一个 NSDate 时,为什么我的 NSDateFormatter 会根据佛教日历格式解析它,即使我正确设置了它的属性(尤其是 <代码>formatter.calendar).我需要将我的 formattedDate 转换为具有公历格式的 NSDate.根据它的逻辑,我希望 NSDateFormatter 给我一个公历格式的日期,但它没有.

When I'm converting the formattedDate (which is an NSString) to an NSDate, why does my NSDateFormatter parse it according to the Buddhist calendar format even if I set it's properties properly (especially the formatter.calendar). I need my formattedDate converted as an NSDate with the Gregorian calendar format. Based on its logic, I'm expecting the NSDateFormatter to give me a date with the Gregorian format but it doesn't.

基本上,我需要遵循来自佛教 NSDate 的格里高利格式的 NSDate.

Basically, I need an NSDate following the Gregorian format from a Buddhist NSDate.

有什么想法吗?

推荐答案

NSDate 只是一个对象包装器,用于指示与参考日期的偏移量的双精度值.它不包含有关日历或时区的信息.这只是一个时间点.

NSDate is just an object wrapper for a double value indicating the offset from the reference date. It does not contain information about calendars or timezones. It's just a point in time.

我们人类决定将这个时间点称为什么取决于我们的日历和时区.一个 NSDate 对象没有日历和时区就没有意义.

What we as humans decide to call this point in time depends on our calendar and timezone. An NSDate object has no meaning without a calendar and timezone.

当您使用 NSLog 打印日期对象时,会调用日期对象上的 -description 方法,以及 -description方法使用您设备的语言环境来格式化字符串.这就是 boomDate 与佛教日历属性一起显示的原因;您的佛教语言环境使用佛教日历.

When you're printing a date object using NSLog, the -description method on your date object gets called, and the -description method formats the string using your device's locale. That's why the boomDate is shown with buddhist calendar properties; your buddhist locale uses a buddhist calendar.

nowboomDate 变量相等(尝试调用 [now isEqualToDate:boomDate]).

The now and boomDate variables are equal (try calling [now isEqualToDate:boomDate]).

您可以看出转换为公历成功,因为 formattedDate 显示的日期与公历中的日期相同.

You can tell that the conversion to gregorian succeeded, because the formattedDate shows the date as it would be in the gregorian calendar.

这篇关于从佛历转换为公历日期格式时,NSDateFormatter 行为不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆