iOS中的日历

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;
}
时间: 2024-08-08 13:57:36

iOS中的日历的相关文章

ios中VRGCalendarView日历控件

http://pan.baidu.com/share/link?shareid=4166002480&uk=923776187 官网 https://github.com/TjeerdVurig/Vurig-Calendar #import <UIKit/UIKit.h> #import "VRGCalendarView.h" @interface ViewController : UIViewController<VRGCalendarViewDelegat

iOS中UITextView方法解读

iOS中UITextView方法解读 常用属性解读: @property(nonatomic,assign) id<UITextViewDelegate> delegate; 设置代理属性 @property(nonatomic,copy) NSString *text; textView上的文本 @property(nonatomic,retain) UIFont *font; 设置文本字体 @property(nonatomic,retain) UIColor *textColor; 设置

iOS中几种数据持久化方案

概论 所谓的持久化,就是将数据保存到硬盘中,使得在应用程序或机器重启后可以继续访问之前保存的数据.在iOS开发中,有很多数据持久化的方案,接下来我将尝试着介绍一下5种方案: plist文件(属性列表) preference(偏好设置) NSKeyedArchiver(归档) SQLite 3 CoreData 沙盒 在介绍各种存储方法之前,有必要说明以下沙盒机制.iOS程序默认情况下只能访问程序自己的目录,这个目录被称为"沙盒". 1.结构 既然沙盒就是一个文件夹,那就看看里面有什么吧

iOS中UIWebView的使用详解

iOS中UIWebView的使用详解 一.初始化与三种加载方式 UIWebView继承与UIView,因此,其初始化方法和一般的view一样,通过alloc和init进行初始化,其加载数据的方式有三种: 第一种: - (void)loadRequest:(NSURLRequest *)request; 这是加载网页最常用的一种方式,通过一个网页URL来进行加载,这个URL可以是远程的也可以是本地的,例如我加载百度的主页:     UIWebView * view = [[UIWebView al

IOS中NSString的常见用法

iOS NSString的常用用法 //1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astring = [[NSString alloc] init]; astring = @"This is a String!"; //3.在以上方法中,提升速度:initWithString方法 NSString *astring = [[NSString allo

iOS中UITableViewCell的重用问题解决方案

UITableViewCell重用 为了能够保证tableViewCell能够高效的执行,Objective-c中引进了重用队列的机制,重影现象也是在重用队列时经常遇到的问题,那么如何解决这个问题呢?下面给出了几种解决办法. 第一种解决方法 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *subViews = cell

iOS 中UIButton的 settitle 和 titlelabel的使用误区

UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state @property(nonatomic,readonly,retain) NSString *currentTitle; @property(n

iOS 中KVC、KVO、NSNotification、delegate 总结及区别

iOS 中KVC.KVO.NSNotification.delegate 总结及区别 1.KVC,即是指 NSKeyValueCoding,一个非正式的Protocol,提供一种机制来间接访问对象的属性.而不是通过调用Setter.Getter方法访问.KVO 就是基于 KVC 实现的关键技术之一. Demo: @interface myPerson : NSObject { NSString*_name; int      _age; int      _height; int      _w

ios中UIControl详解

上篇讲到了UITouch和UIEvent事件,简单回顾一下,UIEvent是一系列UITouch的集合,在IOS中负责响应触摸事件.另外还提到了响应者链的概念,在IOS中,所有事件有一个最先响应者,事件可以沿着响应者链向下传递. 接下来是UIControl对象 UIControl是UIView的子类,当然也是UIResponder的子类.UIControl是诸如UIButton.UISwitch.UITextField等控件的父类,它本身也包含了一些属性和方法,但是不能直接使用UIControl