object-c 把long型的时间转换成date类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了object-c 把long型的时间转换成date类型相关的知识,希望对你有一定的参考价值。

1374741566000这种转换成日期。前面的long型是在java中通过TimeStamp.getTime()得到的

1. 自动类型转换

自动类型转换 : 将一个基本类型变量 赋值给另外一个基本类型变量就会出现基本类型转换;
-- 整型 -> 浮点型 : 除了类型转换为浮点型之外, 不会有太大变化;
-- 浮点型 -> 整型 : 类型转为整型, 小数部分被舍弃;
-- 长整形 -> 整型 : 取值范围变小, 可能发生溢出;

示例 :
-- Object-C 代码 :

[objc] view plaincopy
/*************************************************************************
> File Name: 09_typeAutoConversion.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 9/ 8 11:18:53 2014
************************************************************************/
#import <Foundation/Foundation.h>

int main(int argc, charchar **argv)

@autoreleasepool
/* 定义 int 类型变量 */
int a = 38;
/* 将 int 类型变量转换为 float, 数值没有变化, 只是类型发生了变化 */
float b = a;
/* 打印int -> float 结果, 打印 : 38 */
NSLog(@"b = %g", b);

/* 定义 short 类型变量 */
short c = 38;
/* 将 short 类型变量赋值给 char 变量, short 自动转化为 char 类型 */
char d = c;
/* 打印 short -> char 类型, 打印 : & */
NSLog(@"d = %c", d);

double e = 38.3838;
/* 将 double 类型转为 int 类型, 小数部分自动省略 */
int f = e;
/* 打印 double -> int 类型, 打印 : 38 */
NSLog(@"f = %d", f);

/* 将 double 类型转为char 类型, 小数部分自动省略, 如果数值过大, 整数部分会溢出 */
char g = e;
/* 打印 double -> char, 打印 : & */
NSLog(@"g = %c", g);

int h = 40000;
/* 将 int 类型转为 short 类型, 如果数值过大, 可能会溢出 */
short i = h;
/* 打印 int -> short, 溢出 打印 : -25536 */
NSLog(@"i = %d", i);




-- 编译运行 :

[plain] view plaincopy
octopus-2:oc octopus$ clang -fobjc-arc -framework Foundation 09_typeAutoConversion.m
octopus-2:oc octopus$ ./a.out
2014-09-08 13:08:41.250 a.out[1345:507] b = 38
2014-09-08 13:08:41.252 a.out[1345:507] d = &
2014-09-08 13:08:41.252 a.out[1345:507] f = 38
2014-09-08 13:08:41.253 a.out[1345:507] g = &
2014-09-08 13:08:41.253 a.out[1345:507] i = -25536
octopus-2:oc octopus$

2. 强制类型转换

强制类型转换 : 通过 (typeName) 可以强行指定一个变量的类型;

强制转换示例 :
-- Object-C 代码 :

[objc] view plaincopy
/*************************************************************************
> File Name: 09_typeConversion.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 9/ 8 13:27:52 2014
************************************************************************/

#import <Foundation/Foundation.h>

int main(int argc, charchar * argv[])

@autoreleasepool
int a = 38;
int b = 100;

/* int 类型 与 int 类型相除 还是 int 类型, 结果是 0 */
float c = a / b;

/* 先将 a 转为 float 类型, 再进行计算, 得出的结果就是 float 类型 */
float d = (float)a / b;

/* 将 float 类型转为 int 类型后再计算, 结果是 39 */
int e = (int)38.3838 + (int)1.3838;

NSLog(@"c = %g, d = %g, e = %d", c, d, e );



-- 编译运行 :

[plain] view plaincopy
octopus-2:oc octopus$ ./a.out
2014-09-08 13:31:44.361 a.out[1391:507] c = 0, d = 0.38, e = 39
octopus-2:oc octopus$

3. 类型自动提升

表达式数据类型自动提升规则 :
-- 整型自动提升 : 所有的表达式中得 short 和 char 类型的数据都会被提升为 int 类型;
-- 提升至最高类型 : 算数表达式的数据类型自动提高到表达式中等级最高的数据类型;
-- 类型等级规则 : 从低到高 : short -> int -> long -> longlong -> float -> double -> long double;

代码示例 :
-- Object-C 代码 :

[objc] view plaincopy
/*************************************************************************
> File Name: 09_typeAutoPromote.m
> Author: octopus
> Mail: octopus_truth.163.com
> Created Time: 一 9/ 8 13:44:53 2014
************************************************************************/

#import <Foundation/Foundation.h>

int main(int argc, charchar * argv[])

@autoreleasepool
short a = 37;

/* a - 2 表达式中, a 会自动提升为 int 类型 */
NSLog(@"计算 a - 2 的数据类型大小 : %ld", sizeof(a - 2));

/* 整个表达式的数据类型转换为 double 类型 */
double b = a / 2.0;
NSLog(@"b = %g", b);



-- 编译执行 :

[plain] view plaincopy
octopus-2:oc octopus$ ./a.out
2014-09-08 13:50:27.502 a.out[1418:507] 计算 a - 2 的数据类型大小 : 4
2014-09-08 13:50:27.505 a.out[1418:507] b = 18.5
octopus-2:oc octopus$
参考技术A [_scoll setContentSize:CGSizeMake(320, 800)];
NSDate *date=[NSDate dateWithTimeIntervalSince1970:1374741566];
//日期格式化
NSDateFormatter *format=[[NSDateFormatter alloc]init];
[format setDateFormat:@"yyyy年MM月dd日 hh:mm:ss"];
NSString *nsdate=[format stringFromDate:date];
NSLog(@"%@",nsdate);本回答被提问者采纳

以上是关于object-c 把long型的时间转换成date类型的主要内容,如果未能解决你的问题,请参考以下文章

js中怎么吧long型的日期转换成String类型的

java怎么把long型的数据转化成Integer型的数据?

java中怎么把Long转换成日期格式

如何用Java把date类型转换成long数字?

java编程之怎样把Long转换成Date的日期格式

怎么将long类型转换成date