NSDate和NSDateFormatter 相关应用代码示例
此方法用来计算当前时间与目标时间的先后顺序:
-(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{
//将当前时间转为本地时区
NSDate * currentLocalDate = [self localDateFromDate:currentDate];
NSLog(@"currentDate--->%@",currentLocalDate);
//设置目标时间为当天15:30
NSString *dateStr = [NSString stringWithFormat:@"%@",currentLocalDate];
NSString *dateString = [dateStr substringToIndex:11];
NSString *targetDateStr = [NSString stringWithFormat:@"%@15:30:00",dateString];
NSDateFormatter *formatter001 = [[NSDateFormatter alloc] init];
[formatter001 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSDate * targetDate = [formatter001 dateFromString:targetDateStr];
//将目标时间转为本地时区
targetDate = [self localDateFromDate:targetDate];
NSLog(@"targetDate--->%@",targetDate);
//比较当前时间和目标时间
NSComparisonResult result = [currentLocalDate compare:targetDate];
NSLog(@"result--->%d",result);
//获取当前日期是星期几
NSString *weekDay = [self weekdayStringFromDate:currentDate];
NSLog(@"weekDay--->%@",weekDay);
//计算收益时间
NSDate *calDate;
if ([weekDay isEqualToString:@"周一"] || [weekDay isEqualToString:@"周二"] || [weekDay isEqualToString:@"周三"] ) {
//升序
if (result == NSOrderedAscending) {
NSLog(@"15:30之前");
calDate = [currentDate dateByAddingTimeInterval:aDay];
}
else{
NSLog(@"15:30之后");
calDate = [currentDate dateByAddingTimeInterval:2*aDay];
}
}
else if ([weekDay isEqualToString:@"周四"]){
//升序
if (result == NSOrderedAscending) {
calDate = [currentDate dateByAddingTimeInterval:aDay];
}
else{
calDate = [currentDate dateByAddingTimeInterval:4*aDay];
}
}
else if ([weekDay isEqualToString:@"周五"]){
//升序
if (result == NSOrderedAscending) {
calDate = [currentDate dateByAddingTimeInterval:3*aDay];
}
else{
calDate = [currentDate dateByAddingTimeInterval:4*aDay];
}
}
else if ([weekDay isEqualToString:@"周六"]){
calDate = [currentDate dateByAddingTimeInterval:3*aDay];
}
else if ([weekDay isEqualToString:@"周日"]){
calDate = [currentDate dateByAddingTimeInterval:2*aDay];
}
calDate = [self localDateFromDate:calDate];
return calDate;
}
将时区转为当前时区
- (NSDate *)localDateFromDate:(NSDate *)date{
//获取系统时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
//时间间隔
NSInteger interval = [zone secondsFromGMTForDate: date];
//得到当前时间
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
return localeDate;
}
判断当天是星期几:
- (NSString*)weekdayStringFromDate:(NSDate*)inputDate {
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null],@"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
NSDate和NSDateFormatter 相关应用代码示例的更多相关文章
- 推荐Java五大微服务器及其代码示例教程
来源素文宅博客:http://blog.yoodb.com/yoodb/article/detail/1339 微服务越来越多地用于开发领域,因为开发人员致力于创建更大,更复杂的应用程序,这些应用程序 ...
- Java基础知识强化之IO流笔记72:NIO之 NIO核心组件(NIO使用代码示例)
1.Java NIO 由以下几个核心部分组成: Channels(通道) Buffers(缓冲区) Selectors(选择器) 虽然Java NIO 中除此之外还有很多类和组件,Channel,Bu ...
- Hadoop RCFile存储格式详解(源码分析、代码示例)
RCFile RCFile全称Record Columnar File,列式记录文件,是一种类似于SequenceFile的键值对(Key/Value Pairs)数据文件. 关键词:Reco ...
- Delphi之通过代码示例学习XML解析、StringReplace的用法(异常控制 good)
*Delphi之通过代码示例学习XML解析.StringReplace的用法 这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <? ...
- [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传
原文 [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传 Fine Uploader(http://fineuploader.com/)是一个实现 ajax 上传文件 ...
- 分享用于学习C++音频处理的代码示例
与<分享用于学习C++图像处理的代码示例>为姊妹篇. 为了便于学习C++音频处理并研究音频算法, 俺写了一个适合初学者学习的小小框架. 麻雀虽小五脏俱全,仅仅考虑单通道处理. 采用Deco ...
- JAVA NIO工作原理及代码示例
简介:本文主要介绍了JAVA NIO中的Buffer, Channel, Selector的工作原理以及使用它们的若干注意事项,最后是利用它们实现服务器和客户端通信的代码实例. 欢迎探讨,如有错误敬请 ...
- Ice简介+Qt代码示例
1.ICE是什么? ICE是ZEROC的开源通信协议产品,它的全称是:The Internet Communications Engine,翻译为中文是互联网通信引擎,是一个面向对象的中间件,它封装并 ...
- 英特尔® 实感™ 深度摄像头代码示例 – R200 摄像头数据流
英特尔开发人员专区原文地址 简介 该可下载代码示例展示了如何使用面向 Windows 的英特尔® 实感™ SDK* 捕捉和查看用 C#/XAML 编写的原始 R200 摄像头数据流. Visual S ...
随机推荐
- Mvc自定义路由让支持.html的格式
前言 在大多时候,我们都需要自定义路由,当我们设置为/list/1.html的时候,有的时候会出现以下异常. routes.MapRoute( "category", // 路由名 ...
- 总结Lvs、nginx、Haproxy
!!!! 参考的比较多,自己想法少 大神总结的比较好 ====>http://www.ha97.com/5646.html 特性 LVs Nginx Haproxy 工作层 四层(传输层) 七 ...
- .net DropDownList静态联动
1.前台 <span id="spnClient" style="margin-left: 30px; margin-top: 10px"> < ...
- css引入方式
1.<style> body{} </style> 2.写在一个单独的文件里面保存即新建一个文件:xx.css; 注明该文件的位置<link re ...
- 使用RestTemplate发送post请求
最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...
- Windows Server 2008 R2域控组策略设置禁用USB
问题: Windows Server 2008 R2域控服务器如何禁用客户端使用USB移动存储(客户端操作系统需要 Windows Vista以上的操作系统,XP以下的操作系统不能禁用USB移动存储) ...
- SVN File Name Case Sensitivity--SVN大小写问题
from:http://www.visualsvn.com/support/topic/00019/ Windows has a case-insensitive file system. Unix ...
- yii框架的增删改查
一.新增 使用model::save()操作进行新增数据 $user= new User; $user->username =$username; $user->password =$pa ...
- js ajax同步请求造成浏览器假死的问题
一.问题的起因 今天做一个需求遇到了这么个情况,就是用户个人中心有个功能,点击按钮,可以刷新用户当前的积分,这个肯定需要使用到ajax的同步请求了,当时喀喀喀三下五除二写玩了,大概代码如下: /** ...
- Spark&Hadoop:scala编写spark任务jar包,运行无法识别main函数,怎么办?
昨晚和同事一起看一个scala写的程序,程序都写完了,且在idea上debug运行是ok的.但我们不能调试的方式部署在客户机器上,于是打包吧.打包时,我们是采用把外部引入的五个包(spark-asse ...