iOS世界时间,NSDateFormatter一篇文章全弄懂

来源:互联网 发布:mac下的虚拟机 编辑:程序博客网 时间:2024/06/06 03:38


iOS世界时间,NSDateFormatter一篇文章全弄懂


最近的项目,后台用java ,传的时间  @"2017-05-15T16:00:00.000+0000"是这种格式的,

世界时间格式,可怜的哥虽然从业多年,但一时间也闷逼了,为了维护世界的和平,特意记录一下这些特殊时间格式的显示,以弥补哥从业多年的时间漏洞


// 首当其冲的,是哥的时区转换,太坑了,看哥好说话,哥忍了。

-(void)testDateZone{    NSString *timeDate = [NSString stringWithFormat:@"2017-05-15T16:00:00.000+0000"];    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];    NSDate *localDate = [dateFormatter dateFromString:timeDate];        [dateFormatter setDateFormat:@"yyyy-MM-dd"];    NSString *strDate = [dateFormatter stringFromDate:[self getNowDateFromatAnDate:localDate]];    NSLog(@"strDate = %@",strDate);        [dateFormatter setDateStyle:NSDateFormatterFullStyle];}- (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate{    //设置源日期时区    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT    //设置转换后的目标日期时区    NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];    //得到源日期与世界标准时间的偏移量    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];    //目标日期与本地时区的偏移量    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];    //得到时间偏移量的差值    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;    //转为现在时间    NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];    return destinationDateNow;}



// string to date 
- (NSDate *)stringToDate:(NSString *)strdate{    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    NSDate *retdate = [dateFormatter dateFromString:strdate];    return retdate;}//  date转string- (NSString *)dateToString:(NSDate *)date{    NSDateFormatter *dateFormatter = [[NSDateFormatteralloc] init];    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    NSString *strDate = [dateFormatter stringFromDate:date];    return strDate;}


以下是setDateFormat可使用的英文代号:


纪元的显示:
G:显示AD,也就是公元
年的显示:
yy:年的后面2位数字
yyyy:显示完整的年
月的显示:
M:显示成1~12,1位数或2位数
MM:显示成01~12,不足2位数会补0
MMM:英文月份的缩写,例如:Jan
MMMM:英文月份完整显示,例如:January

日的显示:
d:显示成1~31,1位数或2位数
dd:显示成01~31,不足2位数会补0
星期的显示:
EEE:星期的英文缩写,如Sun
EEEE:星期的英文完整显示,如,Sunday

上/下午的显示:
aa:显示AM或PM

小時的显示:
H:显示成0~23,1位数或2位数(24小时制
HH:显示成00~23,不足2位数会补0(24小时制)
K:显示成0~12,1位数或2位数(12小時制)
KK:显示成0~12,不足2位数会补0(12小时制)

分的显示:
m:显示0~59,1位数或2位数
mm:显示00~59,不足2位数会补0

秒的显示:
s:显示0~59,1位数或2位数
ss:显示00~59,不足2位数会补0
S: 毫秒的显示

时区的显示:
z / zz /zzz :PDT
zzzz:Pacific Daylight Time
Z / ZZ / ZZZ :-0800
ZZZZ:GMT -08:00
v:PT
vvvv:Pacific Time


举个栗子,好重哦:

//    2013-08-03T04:56:52+0000 @"yyyy-MM-dd'T'HH:mm:ssZ"

//    2017-05-15T16:00:00.000+0000 @"yyyy-MM-dd'T'HH:mm:ss.SSSZ"






//        NSDateFormatterNoStyle:不显示日期,时间的风格。////        NSDateFormatterShortStyle:显示短的日期,时间风格////        NSDateFormatterMediumStyle:显示中等的日期,时间风格////        NSDateFormatterLongStyle:显示长的日期,时间风格////        NSDateFormatterFullStyle:显示完整的日期,时间风格//  日期格式器NSDateFormatter-(void)showNSDateFormatterDiff{        //需要被格式化的时间为从1970年1.1开始,20年后的日期    NSDate* dt = [NSDate dateWithTimeIntervalSince1970:20*366*24*3600];    //创建两个NSLocale,分别代表中国,美国    NSLocale* locales[] = {[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"],        [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]};    NSDateFormatter* df[8];    //为上面两个NSLocale创建8个DateFormat对象    for (int i=0; i<2; i++) {        df[i * 4] = [[NSDateFormatter alloc] init];        //设置NSDateFormatter的日期,时间风格        [df[i * 4] setDateStyle:NSDateFormatterShortStyle];        [df[i * 4] setTimeStyle:NSDateFormatterShortStyle];        //设置NSDateFormatter的NSLocale        [df[i * 4] setLocale:locales[i]];                df[i * 4 + 1] = [[NSDateFormatter alloc] init];        //设置NSDateFormatter的日期,时间风格        [df[i * 4 + 1] setDateStyle:NSDateFormatterMediumStyle];        [df[i * 4 + 1] setTimeStyle:NSDateFormatterMediumStyle];        //设置NSDateFormatter的NSLocale        [df[i * 4 + 1] setLocale:locales[i]];                df[i * 4 + 2] = [[NSDateFormatter alloc] init];        //设置NSDateFormatter的日期,时间风格        [df[i * 4 + 2] setDateStyle:NSDateFormatterLongStyle];        [df[i * 4 + 2] setTimeStyle:NSDateFormatterLongStyle];        //设置NSDateFormatter的NSLocale        [df[i * 4 + 2] setLocale:locales[i]];                df[i * 4 + 3] = [[NSDateFormatter alloc] init];        //设置NSDateFormatter的日期,时间风格        [df[i * 4 + 3] setDateStyle:NSDateFormatterFullStyle];        [df[i * 4 + 3] setTimeStyle:NSDateFormatterFullStyle];        //设置NSDateFormatter的NSLocale        [df[i * 4 + 3] setLocale:locales[i]];    }        for (int i=0; i<2; i++) {        switch (i) {            case 0:                NSLog(@"----------中国日期格式----------");                break;            case 1:                NSLog(@"----------美国日期格式----------");                break;                        }        NSLog(@"SHORT格式的日期格式:%@",[df[i * 4] stringFromDate:dt]);        NSLog(@"MEDIUM格式的日期格式:%@",[df[i * 4 + 1] stringFromDate:dt]);        NSLog(@"LONG格式的日期格式:%@",[df[i * 4 + 2] stringFromDate:dt]);        NSLog(@"FULL格式的日期格式:%@",[df[i * 4 + 3] stringFromDate:dt]);    }        NSDateFormatter* df2 = [[NSDateFormatter alloc] init];    //设置自定义的格式器模版    [df2 setDateFormat:@"公元yyyy年MM月DD日 HH时mm分"];    //执行格式化    NSLog(@"%@",[df2 stringFromDate:dt]);        NSString* dateStr = @"2013-03-12";    NSDateFormatter* df3 = [[NSDateFormatter alloc] init];    //根据日期字符串的格式设置格式模版    [df3 setDateFormat:@"yyyy-MM-dd"];    //将字符串转换为NSDate对象    NSDate* date2 = [df3 dateFromString:dateStr];    NSLog(@"%@",date2);     }

上面的输出内容:


    2017-05-2617:36:42.465 NSDateFormatterTest[466:303] ----------中国日期格式----------

    

    2017-05-2617:36:42.467 NSDateFormatterTest[466:303] SHORT格式的日期格式:90-1-16上午8:00

    

    2017-05-2617:36:42.467 NSDateFormatterTest[466:303] MEDIUM格式的日期格式:1990116上午8:00:00

    

    2017-05-2617:36:42.468 NSDateFormatterTest[466:303] LONG格式的日期格式:1990116 GMT+8上午8:00:00

    

    2017-05-2617:36:42.469 NSDateFormatterTest[466:303] FULL格式的日期格式:1990116星期二中国标准时间上午8:00:00

    

    2017-05-2617:36:42.469 NSDateFormatterTest[466:303] ----------美国日期格式----------

    

    2017-05-2617:36:42.470 NSDateFormatterTest[466:303] SHORT格式的日期格式:1/16/90,8:00 AM

    

    2017-05-2617:36:42.470 NSDateFormatterTest[466:303] MEDIUM格式的日期格式:Jan16,1990,8:00:00 AM

    

    2017-05-2617:36:42.471 NSDateFormatterTest[466:303] LONG格式的日期格式:January16,1990 at8:00:00 AM GMT+8

    

    2017-05-2617:36:42.471 NSDateFormatterTest[466:303] FULL格式的日期格式:Tuesday, January16, 1990 at8:00:00 AM China Standard Time

    

    2017-05-2617:36:42.475 NSDateFormatterTest[466:303]公元199001160800

    

    2017-05-2617:36:42.476 NSDateFormatterTest[466:303]2013-03-1116:00:00 +0000




总结:不常用的并不是没用,很多东西,苹果哥哥已经想到了,只是英文文档啊,还是那么坑爹,栗子还少,看的老眼昏花,五骨酸软。








阅读全文
0 0