如何将长整数时间转换为NSString或NSDate来显示时间?

2024-01-20

我从 Java Date 中得到了一个序列号,它可以转换为长整数,例如“1352101337000”。 我遇到的问题是如何将这个长整数解析回NSDate或NSString,以便我可以清楚地知道序列号显示的时间。

有人对这种情况有解决方案吗?


用这个,

NSTimeInterval timeInMiliseconds = [[NSDate date] timeIntervalSince1970];

要把它改回来,

NSDate* date = [NSDate dateWithTimeIntervalSince1970:timeInMiliseconds];

As per 苹果文档 https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html,

NS时间间隔:用于指定时间间隔,以秒为单位。

typedef double NSTimeInterval;

它是 double 类型。

要将日期转换为字符串,

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];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何将长整数时间转换为NSString或NSDate来显示时间? 的相关文章

随机推荐