Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle equaivalent of java System.currentTimeMillis()?

Tags:

time

oracle

I want to be able to store the current time in milliseconds in an Oracle number field. How do I do this via a query?

select systimestamp from dual; 

returns the actual timestamp. Is there anyway that I can convert this into milliseconds the same way Java's System.currentTimeMillis() does?

like image 737
Gaurav Avatar asked May 13 '10 06:05

Gaurav


People also ask

What is currentTimeMillis in Java?

currentTimeMillis() method returns the current time in milliseconds. The unit of time of the return value is a millisecond, the granularity of the value depends on the underlying operating system and may be larger. For example, many operating systems measure time in units of tens of milliseconds.

What is the difference between Sysdate and Systimestamp?

In Oracle, SYSTIMESTAMP is a keyword that returns the system timestamp of the Oracle server. Likewise, SYSDATE is a keyword that returns the system date of the Oracle server.

Is system currentTimeMillis thread safe?

public static long currentTimeMillis() // Returns the current time in milliseconds. Pros: It is thread safe. Thread safety means that if this method is called between two or more different threads, it will not return erroneous results.

Does Oracle date have milliseconds?

Dates do not contain milliseconds, they only go as fine as seconds. You might want to try a timestamp datatype. Valid date range from January 1, 4712 BC, to December 31, 9999 AD. The default format is determined explicitly by the NLS_DATE_FORMAT parameter or implicitly by the NLS_TERRITORY parameter.


2 Answers

this link helps for all languages currentmillis.com for oracle:

SELECT (SYSDATE - TO_DATE('01-01-1970 00:00:00', 'DD-MM-YYYY HH24:MI:SS')) * 24 * 60 * 60 * 1000 FROM DUAL
like image 97
Milad Avatar answered Oct 17 '22 23:10

Milad


The Java function returns the number of milliseconds which have elapsed since a fixed moment in time. That time is midnight on the first day of 1970 UTC, i.e. the start of Unix clock time.

The following function does the same for PL/SQL. It subtracts the current timestamp from the starting point (where ms=1). It extracts the various time components and turns them into seconds. Finally it multiplies everything by 1000 to get the value in milliseconds:

create or replace function current_millisecs 
    return number 
is
    base_point constant timestamp := to_timestamp('01-JAN-1970 00:00:00.000');
    now constant timestamp := systimestamp AT TIME ZONE 'UTC' ;
begin
    return (
                  ((extract(day    from (now-base_point)))*86400)
                + ((extract(hour   from (now-base_point)))*3600)
                + ((extract(minute from (now-base_point)))*60)
                + ((extract(second from (now-base_point))))
           ) * 1000;
end;
/

If you have Java enabled in the database you may find it simpler to create a Java Stored Procedure instead:

create or replace function currentTimeMillis return number as
language java name 'java.lang.System.currentTimeMillis() return java.lang.Integer';
/

Comparison of the two approaches:

SQL> select currentTimeMillis as JAVA
  2         , current_millisecs as PLSQL
  3         , currentTimeMillis - current_millisecs as DIFF
  4  from dual
  5  /

      JAVA      PLSQL       DIFF
---------- ---------- ----------
1.2738E+12 1.2738E+12          0

SQL>

(My thanks go to Simon Nickerson, who spotted the typo in the previous version of my PL/SQL function which produced an anomalous result.)


Incidentally, if you are only interested in time to the nearest centisecond, Oracle has a built-in for that: DBMS_UTILITY.GET_TIME().

like image 23
APC Avatar answered Oct 17 '22 23:10

APC