Oracle将日期转换为数字 [英] Oracle convert Date to Number
问题描述
我想在oracle 11g中将日期转换为数字.
I want to convert date to number in oracle 11g .
日期存储在(12/03/2013 12:23:00).
The date is stored in (12/03/2013 12:23:00).
我想将其转换为Number(日期对应的数字值). 与在Java中一样,我们将date转换为long.
I want to convert this to Number(The date corresponding number value). As in java we convert date to long .
我希望在这里做同样的事情.
I want the same to be done here .
Calendar c = Calendar.getInstance();
c.set(2013, 05, 23, 0, 0, 0);
Date date = c.getTime();
System.out.println("Date is " + date);
long longDate = date.getTime();
System.out.println("Date as long :" + longDate);
Date d = new Date(longDate);
System.out.println("Converted Date :" + d);*
输出为:
**日期是SGT 2013年6月23日00:00:00
**Date is Sun Jun 23 00:00:00 SGT 2013
日期长:1371916800981
Date as long :1371916800981
转换日期:2013年6月23日星期日00:00:00 SGT **
Converted Date :Sun Jun 23 00:00:00 SGT 2013**
现在我想将值存储为1371916800981
Now I want to store value as 1371916800981
推荐答案
我猜想您想要的长数据类型类似于自1970-01-01起的秒数或毫秒数.
I am guessing that the long data type that you want is something like the number of seconds or milliseconds since 1970-01-01.
要实现这一点,只需要一点算术:
To get this requires just a bit of arithmetic:
select (to_date(s1, 'MM/DD/YYYY HH24:MI:SS') -
to_date('1970-01-01', 'YYYY-MM-DD')
) *24*60*60
from (select '12/03/2013 12:23:00' as s1 from dual
) t
我注意到您的结果使用的是当前时间戳.这可能包括此固定日期格式不包括的毫秒数.
I note that your result is using the current time stamp. This might include milliseconds which this constant date format doesn't include.
这篇关于Oracle将日期转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!