如何将长整数时间转换为NSString或NSDate以显示时间? [英] How to convert long-integer time to NSString or NSDate to display time?
本文介绍了如何将长整数时间转换为NSString或NSDate以显示时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从Java Date获得了一个序列号,该序列号转换为长整数,例如"1352101337000". 我遇到的问题是如何将这个长整数解析为NSDate或NSString,以便我可以清楚地知道序列号显示的时间.
I got a serial number form Java Date which convert into long-integer such as "1352101337000". The problem I met is how to analyze this long-integer number back to NSDate or NSString so that I can clear to know what time the serial number is displaying.
有人对此案有解决方案吗?
Do anybody have solution for this case?
推荐答案
使用此
NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970];
要改回它,
NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeInMiliseconds];
根据 Apple文档 ,
NSTimeInterval:用于指定时间间隔,以秒为单位.
NSTimeInterval: Used to specify a time interval, in seconds.
typedef double NSTimeInterval;
它是double类型的.
It is of type double.
要将日期转换为字符串,
To convert a date to string,
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
//Optionally for time zone converstions
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]];
NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance];
[formatter release];
这篇关于如何将长整数时间转换为NSString或NSDate以显示时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文