iOS自带三种日历,公历、佛教日历和日本日历,要设置日历可以进入"设置-通用-语言与地区-日历"设置,我们中国使用的iPhone默认设置成公历。而泰国人使用的iPhone默认设置的日历是佛教日历。这样会导致同样的代码在国内显示正常,去泰国就仿佛穿越了一般。

问题:使用NSDate *today = [NSDate date];获取的当前的时间在国内显示正常是2017年,而到了泰国却变成了2056年,这是为什么呢?即使是时区差别,也不能差如此多呀??

经过仔细思考,发现语言和地区的设置之中有一个地方可以设置日历,进去把公历改成佛教日历,发现原来显示的2017变成了2056。

方法一:

解决办法:提供一种不受时区、系统日历、本地化等限制,获得公历中的正确的年份、月份、时间等的方法。

Global.h

@property (strong,nonatomic) NSCalendar *calendar;
@property (strong,nonatomic) NSDateComponents *components;
@property (copy,nonatomic) NSString *year;
@property (copy,nonatomic) NSString *month;
@property (copy,nonatomic) NSString *day;
@property (copy,nonatomic) NSString *hour;
@property (copy,nonatomic) NSString *minute;
@property (copy,nonatomic) NSString *second;

Global.m

- (NSCalendar *)calendar{
if(!_calendar){
#ifdef __IPHONE_8_0
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//根据Identifer获取公历
#else
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//根据Identifer获取公历
#endif
_calendar.timeZone = [NSTimeZone localTimeZone];//消除时区差异
_calendar.locale = [NSLocale currentLocale];//消除本地化差异,本地化包括语言、表示的日期格式等
} return _calendar;
} - (NSDateComponents *)components{
if (!_components) {
NSDate *date = [NSDate date];
_components = [self.calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday fromDate:date];
} return _components;
} //返回当前年份
- (NSString *)year{
if (!_year) {
_year = [NSString stringWithFormat:@"%04ld",[kGlobal.components year]];
}
return _year;
} //返回当前月份
-(NSString *)month{
if (!_month) {
_month = [NSString stringWithFormat:@"%02ld",[kGlobal.components month]];
}
return _month;
} //返回当前号数
- (NSString *)day{
if (!_day) {
_day = [NSString stringWithFormat:@"%02ld",[kGlobal.components day]];
}
return _day;
} //返回当前的小时
- (NSString *)hour{
if (!_hour) {
_hour = [NSString stringWithFormat:@"%02ld",[kGlobal.components hour]];
}
return _hour;
} //返回当前的分钟
- (NSString *)minute{
if (!_minute) {
_minute = [NSString stringWithFormat:@"%02ld",[kGlobal.components minute]];
}
return _minute;
} //返回当前的秒钟
- (NSString *)second{
if (!_second) {
_second = [NSString stringWithFormat:@"%02ld",[kGlobal.components second]];
}
return _second;
}

2017-02-04更新:

方法二:正常获取公历年月日(系统日历为佛教日历或其他非公历日历)

1.首先创建一个公历日历对象

- (NSCalendar *)calendar{
if(!_calendar){
#ifdef __IPHONE_8_0
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];//根据Identifer获取公历
#else
_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//根据Identifer获取公历
#endif
_calendar.timeZone = [NSTimeZone localTimeZone];//消除时区差异
_calendar.locale = [NSLocale currentLocale];//消除本地化差异,本地化包括语言、表示的日期格式等
} return _calendar;
}

2.在NSDateFormatter格式化对象的时候加上一条语句就行:[formatter setCalendar:self.calendar];

- (NSDateFormatter *)formatter{
if (!_formatter) {
_formatter = [[NSDateFormatter alloc] init];
[_formatter setCalendar:kGlobal.calendar];
[_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
}
return _formatter;
}

总结:NSDateFormatter如果不设置日历对象,默认使用的是系统的日历对象,所以无论你使用什么方式得到正确的公历年月日,如果最后格式化了日期,都会变成系统默认的日历日期,所以

格式化日期的时候需要设置NSDateFormatter的Calendar对象。同理系统默认的UIDatePicker应该也可以这么设置(有待验证)。

iOS中的日历的更多相关文章

  1. ios中VRGCalendarView日历控件

    http://pan.baidu.com/share/link?shareid=4166002480&uk=923776187 官网 https://github.com/TjeerdVuri ...

  2. iOS - OC NSCalendar 日历

    前言 @interface NSCalendar : NSObject <NSCopying, NSSecureCoding> NSCalendar 对世界上现存的常用的历法进行了封装,既 ...

  3. iOS - Swift NSCalendar 日历

    前言 public class NSCalendar : NSObject, NSCopying, NSSecureCoding NSCalendar 对世界上现存的常用的历法进行了封装,既提供了不同 ...

  4. iOS中处理时间的类

      登录|注册     sakulafly的专栏       目录视图 摘要视图 订阅 Markdown博文大赛清新开启    天天爱答题 一大波C币袭来    中国云计算大会演讲议题公布     大 ...

  5. iOS中支付宝集成

    iOS中支付宝集成 如今各种的App中都使用了三方支付的功能,现在将我在使用支付宝支付集成过程的心得分享一下,希望对大家都能有所帮助 要集成一个支付宝支付过程的环境,大致需要: 1>公司:先与支 ...

  6. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  7. 正则表达式在iOS中的运用

    1.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  8. iOS 中的 HotFix 方案总结详解

    相信HotFix大家应该都很熟悉了,今天主要对于最近调研的一些方案做一些总结.iOS中的HotFix方案大致可以分为四种: WaxPatch(Alibaba) Dynamic Framework(Ap ...

  9. iOS中使用正则

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

随机推荐

  1. COGS 1507. [IOI2000]邮局

    1507. [IOI2000]邮局 ★☆   输入文件:postoffice.in   输出文件:postoffice.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] ...

  2. word2vec_basic.py

    ssh://sci@192.168.67.128:22/usr/bin/python3 -u /home/win_pymine_clean/feature_wifi/word2vec_basic.py ...

  3. 【python】-- web开发之HTML

    HTML HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,是一种制作万维网页面标准语言(标记).通俗的讲就是相当于定义统一的一套规则,大家都来遵守他,这样 ...

  4. WiX 中XML引用变量说明

    WiX 安装工程中的XML 文件所引用变量说明: The WiX project supports the following project reference variables: Variabl ...

  5. CentOS iSCSI服务器搭建------Initiator篇

    服务器信息: [root@initiator ~]# cat /etc/redhat-release CentOS release 6.6 (Final) [root@initiator ~]# un ...

  6. 很详细、很移动的Linux makefile 教程

    近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出 ...

  7. mysql复制表结构和内容

    许多时候我们想复制一张表部分或者全部内容,或者只复制一张表的框架,这种需求应该说我们很常见. 其实想要实现这种需求十分简单. 表一: mysql> select * from student_i ...

  8. 项目中对模板和js,css文件进行压缩的处理类

    我们知道,在html的页面中,所有空格和换行符其实都会占据一定的空间,即使使用了gzip压缩,在传输过程中依然会浪费用户的流量和我们自己服务器的带宽,此脚本就是为了解决这个问题而诞生的. 请自行下载G ...

  9. iOS 当前应用或者浏览器中 唤起 手机其他应用

    这种方法 是 产品很常见的需求,关键 是在info.plist  URL types 设置对应属性 比如 里面 子属性 URL identifier  设置成 bundle id   //设置应用指向 ...

  10. c++之默认参数的函数

    默认参数,看个例子就明白了 int add(int a=5,int b=6,z=3): int main(){ add():// 全部默认 add(1,5)://第三个参数默认 add(1,2,3): ...