Objective-c语言_NSDate(时间类型)和NSCalendar(日历)和NSTimeZone(时区)
来源:互联网 发布:mac上装windows虚拟机 编辑:程序博客网 时间:2024/05/17 22:04
#import <Foundation/Foundation.h>
int main(int argc,const char * argv[]) {
@autoreleasepool {
/*
NSDate
在OC中,foundation框架为我们提供强大的时间操作类'NSDate',该类封装了各种处理时间和日期的API
*/
//获取系统当前时间(GMT Greenwich Mean Tine格林尼治标准时间,他与北京时间相差8个小时,北京时间=GTM时间+8);
NSDate *date1=[NSDatedate];
NSLog(@"date=%@",date1);
//当前系统时间+100秒
//NSTimeInterval 是一个以秒为单位的时间片,也叫时间戳
NSDate *date2=[NSDatedateWithTimeIntervalSinceNow:60];//加60秒 -60秒
NSLog(@"date2=%@",date2);
NSDate *date3=[NSDatedateWithTimeIntervalSince1970:0];//从1970年1月1日0时0分开始算
NSLog(@"date3=%@",date3);//1970称为纪元年
//距离sinceDate日期timeIntervald时间的时间
NSDate *date4=[NSDatedateWithTimeInterval:100sinceDate:[NSDatedate]];
//sineDate从哪里开始
NSLog(@"date4=%@",date4);
//通过时间追加创建NSDate创建一个新的时间 同样的返回secondes秒后的时间,如果这个seconds为负的话,就是seconds秒前的时间
NSDate *date5=[date4 dateByAddingTimeInterval:100];
NSLog(@"date5=%@",date5);
//日期比较
//1.两个日期的时间间隔
//将一个入侵转换成时间戳,此时间戳使用1970年为基准
NSTimeInterval timeINterval1970=[[NSDatedate] timeIntervalSince1970];
NSLog(@"timeInterval1970=%f",timeINterval1970);
//将一个时间转换成时间戳,此时间戳是以现在为基准
NSTimeInterval timeIntervalNow=[[NSDatedate] timeIntervalSinceNow];
NSLog(@"timeINtervalNow=%f",timeIntervalNow);
//2.日期的早晚比较
NSDate *dateNow=[NSDatedate];
NSDate *anHourAgo=[dateNow dateByAddingTimeInterval:-3600];
//判断两个日期时间是否相等
BOOL flag=[dateNow isEqualToDate:anHourAgo];
if (flag)
{
NSLog(@"dateNow=anHourAgo");
}
//判断两个日期的先后,返回早晚的日期
NSDate *date6=[dateNow earlierDate:anHourAgo];
if ([date6 isEqualToDate:dateNow])
{
NSLog(@"较早的时间时datrNow");
}
else
{
NSLog(@"较早的时间anHourAgo");
}
//判断两个日期的先后,返回较晚的日期
NSDate *date7=[dateNow laterDate:anHourAgo];
if([date7 isEqualToDate:dateNow])
{
NSLog(@"较晚的时间是dateNow");
}
else
{
NSLog(@"较晚的时间时anHourAgo");
}
//比较两个日期的早晚也可以使用我们之前学过的compare
NSComparisonResult comparisonResult=[dateNowcompare:anHourAgo];
switch (comparisonResult)
{
caseNSOrderedAscending:
{
NSLog(@"dateNow < anHourAgo");
}
break;
case NSOrderedSame:
{
NSLog(@"dateNow = anHourAgo");
}
break;
caseNSOrderedDescending:
{
NSLog(@"dateNow > anHourAgo");
}
break;
default:
break;
}
/*
NSCalendar:日历
我们通过NSTimeInterval创建一个时间很方便,但很不直观,foundation框架为此为我们
提供NSCalendar,NSCalendar由更自然的日期组成,例如日,月,星期,等,可以使用NSDateComponents
和 NSCAlendar来创建一个NSDate对象。
*/
NSCalendar *currentCalendar=[NSCalendarcurrentCalendar];
NSDateComponents *components=[[NSDateComponentsalloc]init];
components.year=2015;
components.month=12;
components.day=17;
components.hour=22;
components.minute=46;
components.second=30;
//输出日期的小时减8了
NSDate *datee=[currentCalendar dateFromComponents:components];
NSLog(@"datee=%@",datee);
//NSTimeZone
//处理日期和时间经常遇到的一个问题就是时区问题,foundation框架提供了NSTimeZone来指定地区日历对象的时区
NSArray *array=[NSTimeZoneknownTimeZoneNames];//列出所有时区
NSLog(@"array=%@",array);
NSTimeZone *timeZone1= [NSTimeZonetimeZoneWithName:@"Asia/Tokyo"];//可以指定名称参数创建一个时区
//可以指定时区缩写创建一个时区
NSTimeZone *time = [NSTimeZonetimeZoneWithAbbreviation:@"PRC"];
//如何将NSDate转换成 NSString ? NSDatrFormatter默认转换问哦本地时区时间
NSDateFormatter *dateFormatter=[NSDateFormatternew];
//设置时间
dateFormatter.timeZone=[NSTimeZonesystemTimeZone];
//设置时间输出格式
//[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
// P193
NSString *string=[dateFormatter stringFromDate:[NSDate date]];
NSLog(@"string=%@",string);
//=============================
//将时间类型的字符串转换成日期
NSString *string1=@"1970-01-01 8:00:00";
[dateFormatter setTimeZone:[NSTimeZonetimeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//得到的时间时GMT时间格林时间
NSDate *date8=[dateFormatter dateFromString:string1];//这里天字符串的时间
NSLog(@"date8=%@",date8);
NSTimeInterval timeInterval=date8.timeIntervalSinceNow;
NSLog(@"距今已经%f秒了",-timeInterval);
timeInterval =-timeInterval/60;
NSLog(@"%f",timeInterval);
// [[NSDate date] timeIntervalSince1970];
NSLog(@"%f",[[NSDatedate]timeIntervalSince1970]);
}
return 0;
}
- Objective-c语言_NSDate(时间类型)和NSCalendar(日历)和NSTimeZone(时区)
- Objective-C_语言_NSDate(时间类型)和NSCalendar(日历)NSTimeZone(时区)
- c 语言获取系统时间和世界各时区时间
- 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)
- 时区日期处理(NSDate, NSCalendar, NSTimeZone)实例详解
- IOS 时区日期处理及定时 (NSDate,NSCalendar,NSTimer,NSTimeZone)
- 时区日期处理(NSDate, NSCalendar, NSTimeZone)实例详解
- ios 日历简单制作和可选择某段特定时间NSCalendar 和NSDate
- OC_时间NSDate、日历NSCalendar
- iOS时间处理(NSDate、NSDateComponents、NSCalendar、NSDateFormatter、NSTimeZone)
- 更改Ubuntu Server命令行下的默认语言类型和时区时间
- 时区 和 时间
- linux时间和时区
- 时间和时区
- 修改时区和时间
- Linux时区和时间
- 设置时间和时区
- 漫谈时间和时区
- Wix打包系列(一)如何使用wix制作安装程序
- Docker Swarm
- 南通大学自动评教脚本(python)
- CAPI&CAPICOM的一些知识[转]
- [Java异常]ClassNotFoundException in Java
- Objective-c语言_NSDate(时间类型)和NSCalendar(日历)和NSTimeZone(时区)
- iOS常用基础框架和第三方类库
- http://weng851217.blog.163.com/blog/static/884996120101128102441359/
- PHPMailer使用说明
- 这又是谁家的网盘?
- memcached使用总结篇<一>
- Responder object
- ION概念及原理
- 2015.12.17小结-数据库级联操作cascade以及部分注解