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. python模块, 包的初识

    Python 模块(Module), 是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代 ...

  2. SQL SERVER 查看表是否存在

    查看表是否存在 if exists(select 1 from sysobjects where id = OBJECT_ID('数据库名称.dbo.表明称')) drop table 为字段添加注释 ...

  3. nginx学习之压缩解压篇(七)

    1.简介 压缩响应可以减少传输数据的大小,节省带宽.但过多的压缩会造成很大的处理开销.在发送给客户端之前,nginx会对响应做压缩,但是如果后端服务器已经 压缩过了,nginx就不再压缩. 2.开启压 ...

  4. java.time.format.DateTimeFormatter

    Java的日期与时间 DateTimeFormatter类是Java 8中日期时间功能里,用于解析和格式化日期时间的类,位于java.time.format包下.   1.预定义的DateTimeFo ...

  5. Android异步载入全解析之使用AsyncTask

    Android异步载入全解析之使用AsyncTask 概述 既然前面提到了多线程,就不得不提到线程池,通过线程池,不仅能够对并发线程进行管理.更能够提高他们运行的效率.优化整个App.当然我们能够自己 ...

  6. LeetCode:算法特辑——二分搜索

    LeetCode:算法特辑——二分搜索 算法模板——基础 int L =0; int R =arr.length; while(L<R) { int M = (R-L)/2+L; if(arr[ ...

  7. 20145229实验三实验报告——敏捷开发与XP实践

    20145229实验三实验报告--敏捷开发与XP实践 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 ** 实验步骤**### 敏捷开发与XP 软件工程包括下列领域:软件需求 ...

  8. shell 查看系统有关信息

    磁盘: 查看磁盘空间或者挂载情况 df -ah 或者 df -h 内存: 查看内存使用情况 free -m total used free shared buffers cached Mem: -/+ ...

  9. CDH- 测试mr

    cdh的mr样例算法的jar包在 [zc.lee@ip---- hadoop-0.20-mapreduce]$ pwd /opt/cloudera/parcels/CDH--.cdh5./lib/ha ...

  10. Codeforces 900D Unusual Sequences:记忆化搜索

    题目链接:http://codeforces.com/problemset/problem/900/D 题意: 给定x,y,问你有多少个数列a满足gcd(a[i]) = x 且 ∑(a[i]) = y ...