iOS 和Android中的基本日期处理
提到日期处理,主要有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中的基本日期处理的更多相关文章
- iOS 和Android中的正则表达式简单使用
ios 中需要使用NSRegularExpression类,NSTextCheckingResult类. 下面给出最基本的实现代码 NSRegularExpression *regex = [NSRe ...
- iOS 和 Android 中的Alert
iOS 和 Android中都有alert这种提示框,下面简单介绍下. ios中的alert叫做UIAlertView,共有4种样式,由于在ios7上,自定义alertview不太好用,所以也就这4种 ...
- iOS 和 Android 中的后台运行问题
后台机制的不同,算是iOS 和 Android的一大区别了,最近发布的iOS7又对后台处理做了一定的更改,找时间总结一下编码上的区别,先做个记录. 先看看iOS的把,首先需要仔细阅读一下Apple的官 ...
- Android中的时间日期选择器
1.layout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- iOS实现Android中Gone的功能
实现隐藏view但不占位置的需求是很常见的(Android里的view.GONE),可iOS里并没有这玩意,只有hidden.于是自己写了一个一般情况下用的category,特殊情况就得看情况做了.其 ...
- 【动画消消乐 】仿ios、android中常见的一个loading动画 074
前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...
- Android中仿IOS提示框的实现
前言 在Android开发中,我们有时需要实现类似IOS的对话框.今天我就来总结下,如何通过自定义的开发来实现类似的功能. 自定义Dialog 我们知道Android中最常用的对话框就是Dialog及 ...
- Ionic中使用Chart.js进行图表展示以及在iOS/Android中的性能差异
Angular Chart 简介 在之前的文章中介绍了使用 Ionic 开发跨平台(iOS & Android)应用中遇到的一些问题的解决方案. 在更新0.1.3版本的过程中遇到了需要使用图表 ...
- iOS中的NSTimer 和 Android 中的Timer
首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...
随机推荐
- poj3107 树形dp
好久没更了.前段时间去ec-final,实力水一波,混了个铜,虽然很弱,但是可以算是对之前一段时间的回报吧. 现在每天忙着复习,逃课太多,啥都不会...不想挂科啊!!Orz... 题意(简化):警察想 ...
- struts2理解
(1) Struts2(一)---struts2的环境搭建及实例 (2) struts2(二)---ModelDriven模型驱动 (3) Struts2属性驱动与模型驱动 (4)
- 【Gym 100610A】Alien Communication Masterclass
题 Andrea is a famous science fiction writer, who runs masterclasses for her beloved readers. The mos ...
- PowerDesigner反向数据库时遇到[Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句。SQLSTATE = 37错误解决方法
逆向工程中,有时会出现如下错误 ... [Microsoft][ODBC SQL Server Driver][SQL Server]无法预定义语句 SQLSTATE = 37000 解决方案: 1. ...
- Codeforces 556D Restructuring Company
传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...
- POJ2823Sliding Window
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 49919 Accepted: 14391 ...
- hdu 1049 Climbing Worm
解题思路: 1. 两种情况,0x1:井深度小于一次跳的高度.0x2:井深度大于一次跳的高度 2.如果 属于 0x1 则一次跳出 3.否则 本次解题中直接枚举跳的次数 一直循环,直到 [每次跳的真实高度 ...
- [转载]给Jquery动态添加的元素添加事件
原文地址:给Jquery动态添加的元素添加事件作者:小飞侠 我想很多人都会向我一样曾经 被新元素的事件绑定困惑很久也就是在页面加载完成后给元素绑定了事件,但又新增加的元素上却没有绑定任何事件. js的 ...
- Ioc注解
注解: 添加注解时,需要添加context的相关 <?xml version="1.0" encoding="UTF-8"?> <beans ...
- VO(DTO)模式在架构设计中是否需要
DTO(VO):也就是一般意义上的VO,封装后的对象.一般用于Web层—Service层间的数据传输入. PO:也就是一般概念上的Domain Object,如hibernate 中的Entity.一 ...