提到日期处理,主要有2个参数,一个是所在的时区,一个是所用的日历方法。

主要涉及2大类问题,一类是日期类型和字符串之间的转化,另一类是日期的计算问题。ios和android都提供了相应的类来处理问题。

iOS

1. NSDateFormatter类

它的作用是进行NSDate 和字符串之间的相互转化。除了自定义格式外,它还提供了集中默认格式常量,例如

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"formattedDateString: %@", formattedDateString);
// Output for locale en_US: "formattedDateString: Jan 2, 2001".

可以注意到,date的格式和time的格式是分开处理的。

除了几种默认样式,ios还提供了+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale

方法,使用这个方法可以产生一个format字符串,这个字符串是系统根据你要显示的内容和地方自动产生的,比自己写的更加准确(因为你可能不知道那个地方的准确日期格式),比如

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSString *usFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options: locale:usLocale];
NSLog(@"usFormatterString: %@", usFormatString);
// Output: usFormatterString: EEE, MMM d.
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
NSString *gbFormatString = [NSDateFormatter dateFormatFromTemplate:@"EdMMM" options: locale:gbLocale];
NSLog(@"gbFormatterString: %@", gbFormatString);
// Output: gbFormatterString: EEE d MMM.

2. NSCalendar类

它的作用是日期计算。

---------------------------

下面的例子可以看到这两个类的基本应用

- (NSString *)getAgeFromBirthday:(NSString *)birthday
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; NSLocale *formatLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
[formatter setLocale:formatLocale]; NSString *formatString = @"dd/MM/yyyy";
[formatter setDateFormat:formatString]; // [formatter setTimeZone:tzTimeZone];
NSLog(@"time zone is %@",formatter.timeZone);
// formatter.timeZone NSDate *birthdayDate = [formatter dateFromString:birthday]; NSDate * newDate =[NSDate new]; NSCalendar* chineseClendar = [ [ NSCalendar alloc ] initWithCalendarIdentifier:NSGregorianCalendar]; NSUInteger unitFlags = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit; NSDateComponents *cps = [chineseClendar components:unitFlags fromDate:birthdayDate toDate:newDate options:];
/*
NSInteger diffHour = [ cps hour ]; NSInteger diffMin = [ cps minute ]; NSInteger diffSec = [ cps second ]; NSInteger diffDay = [ cps day ]; NSInteger diffMon = [ cps month ];
*/
NSInteger diffYear = [ cps year ]; NSString *returnStr = [NSString stringWithFormat:@"%d Years",diffYear]; return returnStr;
}

 2 Android

Android 中常使用SimpleDateFormat,注意这里的locale参数,它和ios里的意义一致,当locale是中国时,日期字符串中会出现 “年,月,日”等中国汉字!ios是否会这样还没有验证。

 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
Date selectDate = null;
try {
selectDate = format.parse(dateStr);
} catch (ParseException e) {
e.printStackTrace();
}

Calendar 的基本使用方法如下:

   Calendar selectCalendar = Calendar.getInstance();
selectCalendar.setTime(selectDate); Calendar nowEndCalendar = Calendar.getInstance();
nowEndCalendar.setTime(new Date());
nowEndCalendar.set(Calendar.HOUR_OF_DAY, 23);
nowEndCalendar.set(Calendar.MINUTE, 59);
nowEndCalendar.set(Calendar.SECOND, 59); if (selectCalendar.compareTo(nowEndCalendar) > 0)
return true;

iOS 和Android中的基本日期处理的更多相关文章

  1. iOS 和Android中的正则表达式简单使用

    ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...

  2. iOS 和 Android 中的Alert

    iOS 和 Android中都有alert这种提示框,下面简单介绍下. ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种 ...

  3. iOS 和 Android 中的后台运行问题

    后台机制的不同,算是iOS 和 Android的一大区别了,最近发布的iOS7又对后台处理做了一定的更改,找时间总结一下编码上的区别,先做个记录. 先看看iOS的把,首先需要仔细阅读一下Apple的官 ...

  4. Android中的时间日期选择器

    1.layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  5. iOS实现Android中Gone的功能

    实现隐藏view但不占位置的需求是很常见的(Android里的view.GONE),可iOS里并没有这玩意,只有hidden.于是自己写了一个一般情况下用的category,特殊情况就得看情况做了.其 ...

  6. 【动画消消乐 】仿ios、android中常见的一个loading动画 074

    前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出-   自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...

  7. Android中仿IOS提示框的实现

    前言 在Android开发中,我们有时需要实现类似IOS的对话框.今天我就来总结下,如何通过自定义的开发来实现类似的功能. 自定义Dialog 我们知道Android中最常用的对话框就是Dialog及 ...

  8. Ionic中使用Chart.js进行图表展示以及在iOS/Android中的性能差异

    Angular Chart 简介 在之前的文章中介绍了使用 Ionic 开发跨平台(iOS & Android)应用中遇到的一些问题的解决方案. 在更新0.1.3版本的过程中遇到了需要使用图表 ...

  9. iOS中的NSTimer 和 Android 中的Timer

    首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...

随机推荐

  1. Html-Css-iframe的自适应高度方案

    先看一个示例,有两个页面,1.html通过iframe嵌入2.html,两个页面都是同域的 a.html <!DOCTYPE html> <html> <head> ...

  2. Java-集合类汇总

    结构图: Collection ├List │├LinkedList │├ArrayList │└Vector │ └Stack └Set Map ├Hashtable ├HashMap └WeakH ...

  3. Linux System Reinforcement、Intrusion Detection Based On syslog

    目录 .文件系统及访问权限 . Linux Syslog . Linux日志审计 . 帐号安全管理 . 基础物理安全 . 系统编译环境安全 . 系统病毒.后门.rootkit安全 . 系统端口.服务安 ...

  4. IRP IO_STACK_LOCATION 《寒江独钓》内核学习笔记(1)

    在学习内核过滤驱动的过程中,遇到了大量的涉及IRP操作的代码,这里有必要对IRP的数据结构和与之相关的API函数做一下笔记. 1. 相关阅读资料 <深入解析 windows 操作系统(第4版,中 ...

  5. Java中使用split、sort函数

    public static void main(String[] args) { // TODO Auto-generated method stub String str = null ; Scan ...

  6. PDP 有多种定义,具体哪一种还需研究!!!!

    PDP (用户面进行隧道转发的信息的保存协议) 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 即PDP上下文,保存用户面进行隧道转发的所有信息,包括RNC/GGSN的 ...

  7. apache 配置多个虚拟主机

    修改文件:httd.conf 文件地址:D:\wamp\bin\apache\Apache2.2.21\conf #配置虚拟主机<VirtualHost 127.0.0.3:80>Serv ...

  8. 5 Tips for creating good code every day; or how to become a good software developer

    Being a good developer is like being any other good professional, it’s all it’s about doing as much ...

  9. 基于Redis的短链接设计思路

    [Markdown阅读][1] 今天上班的时候收到一个需要短链接的需求,之前的做法都是使用了新浪的短链接API(https://api.weibo.com/2/short_url/shorten.js ...

  10. 第三方平台正式支持接入微信公众平台JS-SDK

    之前微信公众平台面向开发者开放微信内网页开发工具包,现在第三方平台也能正式支持接入微信公众平台JS-SDK了,第三方平台可以在获得公众号的授权后,通过JS-SDK帮助公众号开发和实现网页业务. 公众号 ...