java时间戳求时间差_时间戳换算后,截取时间相差8小时的问题
今天在做工程的時候出現(xiàn)了一個問題:就是把我們平時常用的時間戳轉(zhuǎn)換成時間格式,只需要后面的時間部分,不要年月日部分。。。
我的做法是先把時間戳轉(zhuǎn)換成NSDate,再把NSDate轉(zhuǎn)換成字符型,截取后面的時間部分,即可。如下:
//時間戳::
NSString* timeSp = @"7200";
NSDate *currentTime = [NSDate dateWithTimeIntervalSince1970:[timeSp intValue]];
NSLog(@"currentTime : %@", (NSString*)currentTime );
輸出的結(jié)果是: currentTime : 1970-01-01 02:00:00 +0000
在把currentTime轉(zhuǎn)換成NSString類型時候發(fā)現(xiàn),他會多出8個小時。。因為我要做的是一個倒計時的東西,所以我做了以前的處理去掉8小時的時差
NSDate *currentTime = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];
NSLog(@"currentTime : %@", (NSString*)currentTime );
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: currentTime];
NSDate *localeDate = [currentTime? dateByAddingTimeInterval: -interval];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *strDate = [dateFormatter stringFromDate:localeDate];
NSArray* array =? [strDate componentsSeparatedByString:@" "];
NSLog(@"array === %@",array[1]);
NSLog(@"%@", strDate);
這只是我個人的一些做法,,,希望能幫助到遇到相同問題的童鞋。。一起進步
總結(jié)
以上是生活随笔為你收集整理的java时间戳求时间差_时间戳换算后,截取时间相差8小时的问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: weblogic java虚拟机_web
- 下一篇: 微软的system类库和java_Jav