我从这种格式的网络服务中获取一个字符串,pubDate:“星期五,2013年7月19日07:13:44 GMT” 我想打印HH:mm:ss格式的时间,即我要打印07:13:44。 我怎么能做到这一点?
答案 0 :(得分:1)
使用此:
NSString *str = @"Fri, 19 Jul 2013 07:13:44 GMT";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EE, dd MMM yyyy HH:mm:ss z"];
NSDate *date = [df dateFromString:str];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[df setTimeZone:sourceTimeZone];
NSDate *dateFromString = [df dateFromString:str];
[df setDateFormat:@"HH:mm:ss"];
// This is the time you need
NSString *timeIs = [df stringFromDate:date];
答案 1 :(得分:1)
NSString * pubDateString = [NSString stringWithFormat:@“星期五,2013年7月19日07:13:44 GMT”];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
NSDate *pubDate = [df dateFromString:pubDateString];
[df setDateFormat:@"HH:mm:ss"];
[df setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
NSLog(@"%@",[df stringFromDate:pubDate]);
输出:07:13:44
Apple的日期格式指南: http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html