android仿qq空间说说,仿QQ空间发说说的时间显示

起始本人使用的是以下格式输出时间显示的

- (NSString *) compareCurrentTime:(NSString *)str

{

//把字符串转为NSdate

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];

[dateFormatter setTimeZone:timeZone];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *timeDate = [dateFormatter dateFromString:str];

//得到与当前时间差

NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];

timeInterval = -timeInterval;

long temp = 0;

NSString *result;

if (timeInterval < 60) {

result = [NSString stringWithFormat:@"刚刚"];

}

else if((temp = timeInterval/60) <60){

result = [NSString stringWithFormat:@"%ld分钟前",temp];

}

else if((temp = temp/60) <24){

result = [NSString stringWithFormat:@"%ld小时前",temp];

}

else if((temp = temp/24) <30){

result = [NSString stringWithFormat:@"%ld天前",temp];

}

else if((temp = temp/30) <12){

result = [NSString stringWithFormat:@"%ld月前",temp];

}

else{

temp = temp/12;

result = [NSString stringWithFormat:@"%ld年前",temp];

}

return result;

}

之后查找资料后发现一种更好的时间格式显示效果,便记录于此。原文于此

- (NSString *)format:(NSString *)string{

NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];

[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];

[inputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate*inputDate = [inputFormatter dateFromString:string];

//NSLog(@"startDate= %@", inputDate);

NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];

[outputFormatter setLocale:[NSLocale currentLocale]];

[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

//get date str

NSString *str= [outputFormatter stringFromDate:inputDate];

//str to nsdate

NSDate *strDate = [outputFormatter dateFromString:str];

//修正8小时的差时

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: strDate];

NSDate *endDate = [strDate dateByAddingTimeInterval: interval];

//NSLog(@"endDate:%@",endDate);

NSString *lastTime = [self compareDate:endDate];

NSLog(@"lastTime = %@",lastTime);

return lastTime;

}

-(NSString *)compareDate:(NSDate *)date{

NSTimeInterval secondsPerDay = 24 * 60 * 60;

//修正8小时之差

NSDate *date1 = [NSDate date];

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSInteger interval = [zone secondsFromGMTForDate: date1];

NSDate *localeDate = [date1 dateByAddingTimeInterval: interval];

//NSLog(@"nowdate=%@\nolddate = %@",localeDate,date);

NSDate *today = localeDate;

NSDate *yesterday,*beforeOfYesterday;

//今年

NSString *toYears;

toYears = [[today description] substringToIndex:4];

yesterday = [today dateByAddingTimeInterval: -secondsPerDay];

beforeOfYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay];

// 10 first characters of description is the calendar date:

NSString *todayString = [[today description] substringToIndex:10];

NSString *yesterdayString = [[yesterday description] substringToIndex:10];

NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10];

NSString *dateString = [[date description] substringToIndex:10];

NSString *dateYears = [[date description] substringToIndex:4];

NSString *dateContent;

if ([dateYears isEqualToString:toYears]) {//同一年

//今 昨 前天的时间

NSString *time = [[date description] substringWithRange:(NSRange){11,5}];

//其他时间

NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];

if ([dateString isEqualToString:todayString]){

dateContent = [NSString stringWithFormat:@"今天 %@",time];

return dateContent;

} else if ([dateString isEqualToString:yesterdayString]){

dateContent = [NSString stringWithFormat:@"昨天 %@",time];

return dateContent;

}else if ([dateString isEqualToString:beforeOfYesterdayString]){

dateContent = [NSString stringWithFormat:@"前天 %@",time];

return dateContent;

}else{

return time2;

}

}else{

return dateString;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值