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. [JavaScript] Imitate String.Format() in c#

    Definition if (!String.prototype.format) { String.prototype.format = function () { var args = argume ...

  2. 九度OJ 1326:Waiting in Line(排队) (模拟)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:220 解决:64 题目描述: Suppose a bank has N windows open for service. There is ...

  3. ConfigurableBeanFactory

    ConfigurableBeanFactory :关系如下 在上面这样的一个关系图中可以先看下SingletonBeanRegistry的源代码: package org.springframewor ...

  4. 【python】-- 函数非固定参数,返回值(return)

    函数非固定参数 1.默认参数: 代码如下: def information_register(name,age,country,sex): print("----注册信息------&quo ...

  5. HTML/CSS/JS初始化

    CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...

  6. struts2 获取表单数据封装到list和map集合

    一.获取封装表单数据到list集合 示例 获取用户输入的用户名和密码并输出用户名. jsp页面 list[0]表示list中的第一个user对象 Java代码 二.封装表单数据到map集合 示例 获取 ...

  7. codeforces Gravity Flip 题解

    版权声明:本文作者靖心,靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...

  8. Computer Vision: Algorithms and ApplicationsのImage processing

    实在是太喜欢Richard Szeliski的这本书了.每一章节(after chapter3)都详述了该研究方向比較新的成果.还有很多很多的reference,假设你感兴趣.全然能够看那些參考论文 ...

  9. 深入理解ByteBuffer(转)

    转:http://blog.csdn.net/workformywork/article/details/26699345?utm_source=tuicool&utm_medium=refe ...

  10. java 死锁产生原因及解锁(转)

    原文地址 进程死锁及解决办法 一.要点提示 (1) 掌握死锁的概念和产生死锁的根本原因. (2) 理解产生死锁的必要条件--以下四个条件同时具备:互斥条件.不可抢占条件.占有且申请条件.循环等待条件. ...