我想在java中获得类似Date()。getTime()的NSDate,自1970年以来获得毫秒数 但是当我这样做时,我失去了精确度,我得到了一个不同的日期
NSDate *nowDate = [NSDate date];
NSNumber *nowLong = [[NSNumber numberWithFloat: 1000.0 * [nowDate timeIntervalSince1970]] retain];
NSLog(@"nowDate %@", nowDate);
NSLog(@"nowLong %f", [nowLong doubleValue]);
NSNumber *nowNumber = [NSNumber numberWithDouble:([nowLong doubleValue]/1000.0)];
NSTimeInterval timeInterval = [nowNumber doubleValue];
NSDate *nowDateFromLong = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSLog(@"nowDateFromLong %@", nowDateFromLong);
//输出控制台
nowDate 2012-03-23 18:34:42 +0000
nowLong 1332527628288.000000
nowDateFromLong 2012-03-23 18:33:48 +0000
我真的需要像java那样以毫秒为单位获取日期,所以任何帮助都会受到赞赏!
答案 0 :(得分:1)
只需转换为double,而不是float:
NSNumber * nowLong = [[NSNumber numberWithDouble:1000.0 * [nowDate timeIntervalSince1970]] retain];