处理时间相差8个小时的方法
来源:互联网 发布:nodejs centos 代码 编辑:程序博客网 时间:2024/06/05 11:40
[NSDate date]获取的是GMT时间,这个时间和北京时间相差8个小时,以下代码可以解决这个问题
//方法一
- (void)tDate
{
NSDate *date = [NSDatedate];
NSTimeZone *zone = [NSTimeZonesystemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
NSLog(@"%@", localeDate);
}
方法二
- (NSString*)dateAsString:(NSDate*)date
{
NSString *returnValue = @"";
NSDateFormatter *dateFormatter = nil;
if (date != nil) {
if (dateFormatter ==nil) {
dateFormatter = [[NSDateFormatteralloc]init];
}
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setLocale:[NSLocalecurrentLocale]];
returnValue = [dateFormatter stringFromDate:date];
}
return returnValue;
}
//方法三
+ (NSString *)fixStringForDate:(NSDate *)date
{
NSDateFormatter* dateFormatter = [[NSDateFormatteralloc]init];
[dateFormatter setDateStyle:kCFDateFormatterFullStyle];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *fixString = [dateFormatter stringFromDate:date];
[dateFormatter release];
return fixString;
}
//方法四
NSDateComponents *comps = [[NSDateComponentsalloc]init];
[comps setYear:2010];
[comps setMonth:8];
[comps setDay:24];
[comps setHour:17];
[comps setMinute:5];
[comps setTimeZone: [NSTimeZonetimeZoneWithAbbreviation:@"UTC"]];
NSLog(@"%@", [NSTimeZonetimeZoneWithAbbreviation:@"UTC"]);
NSCalendar *cal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *referenceTime = [cal dateFromComponents:comps];
NSLog(@"%@", referenceTime);
- 处理时间相差8个小时的方法
- java时间相差8个小时的解决方法
- 解决时间相差8小时 && 与当前时间相差n个月、n天的代码
- 时间相差8小时
- 如何解决weblogic时间与系统时间相差8个小时的问题!
- tomcat控制台显示的日志时间与当前时间相差8个小时
- iOS开发获取UIDatePicker 的时间与获取时间相差8个小时如何解?
- 双系统,Ubuntu时间不对,相差8个小时
- 获取的NSDate时间和实际相差8个小时的解决方案
- iOS-获取的NSDate date时间与实际相差8个小时解决方案
- iOS-获取的NSDate date时间与实际相差8个小时解决方案
- iOS-获取的NSDate date时间与实际相差8个小时解决方案
- ios开发点滴-关于UIDatePicker 时间相差8个小时的问题
- 获取的[NSDate date]时间与实际相差8个小时解决方案
- iOS-获取的NSDate date时间与实际相差8个小时解决方案
- iOS-获取的NSDate date时间与实际相差8个小时解决方案
- mongoDB对时间的处理ISODate与我们时区相差8小时
- phpstudy 获取时间相差8小时 调整方法
- ubuntu 下使用source_insight的脚本
- mongodb安装运行
- Unix目录结构的来历
- Android新的menu实现——ActionMode
- PopupWindow动画结束后dismiss崩溃解决
- 处理时间相差8个小时的方法
- jvm 内存模型结构
- ZeroMemory和memset的联系和区别
- eclipse中svn插件的安装
- Protocol Buffers 概述
- left join on and 与 left join on where的区别
- android 控件放大缩小效果实现
- VC++ 跟随父窗口调整控件大小
- linux||mac os如何自定义shell命令