Convert ORACLE NUMBER field to ORACLE DATE - java

I have a problem with converting NUMBER field to ORACLE date.
The problem is field holds a value 1285666505575 which consists of 13 digits.
I thought that this is standard timestamp value but current timestamp time consists of 10 digits (check it here).
The field is set from JAVA code.
I would like to convert this number to dd-mm-yyyy human format.
Could you give some suitable advises?
Thanks in advance!
With help of #Jesper i found this solution.
select TO_DATE('01/01/1970 00:00:00','DD/MM/YYYY HH24:MI:SS') + (1285666505575 /1000/60/60/24) from dual

The Unix timestamp (that your link refers to) counts the number of seconds since 01-01-1970.
Java counts the time using a number of milliseconds since 01-01-1970. So it's not a surprise that this has three digits more than the Unix timestamp.
You can pass that number to the constructor of java.util.Date. Example:
Date date = new Date(1285666505575L);
System.out.println(new SimpleDateFormat("dd/MM/yyyy").format(date));

Related

Convert 18 decimal Julian Timestamp with Java

I need to convert what I think is a Julian timestamp to a regular time stamp with Java.
The application that generates this timestamp is a proprietary payment system (Base24-EPS from ACI). I need to be able to pull and parse the value from the database with a Java application.
A sample timestamp value in decimal is 18 digits:
137955731472778910
With DALCI (internal tool provided by Base24-EPS), I can see this is equivalent of:
3: convert 137955731472778910 to datetime(yyyy/mm/dd hh:mm:ss);
2019/12/14 12:39:07
I found an answer here which seems to be related. But 137955731472778910 is smaller than 210866803200000000, which is the Julian timestamp for 01-JAN-1970 (epoch for unix time).
All the other Julian timestamp online converter I see, for example http://www.onlineconversion.com/julian_date.htm, have Julian date format as double 2458806.52903.
18 digits seem too long.
Do you know how can I parse this timestamp format with Java?
Many thanks.
Assuming you are in the UTC timezone (you probably aren't, but you haven't told me what timezone you are in), I have a formula:
long timestampFromAci = ...;
long timestampJava = (timestamp - 122192460002790000L) / 10000;
Instant.ofEpochMilli(timestampJava);
new Date(timestampJava); // Old, deprecated - use java.time classes
This assumes that the conversion is linear.
Your product timestamp has 10000 units per millisecond, since there are 2145052000 milliseconds between 2019/11/19 16:48:15 and 2019/12/14 12:39:07, and the difference in your product's timestamp is 21450514084700.
If you divide these two, that's almost exactly 10000 - the difference is because your tool doesn't display fractional seconds.
Extrapolating from that, I can derive that value that your product timestamp would have for the Unix epoch op 1/1/1970 - 122192460002790000.
However, as I said, I made the assumption that you are in the UTC timezone. For every hour that your timezone is off from UTC, you need to adjust that number by 3600 seconds times 10,000,000 units product timestamp units per second.

Mongodb Epoch Date - java mapping

I would like to store dates in mongodb in epoch format (Unix time in secs or millisec, eg : "1433323417000") and have an object mapping in java java.util.Date Format (or joda.time.DateTime).
Can you let me know if this is possible ?
Thanks in advance
From MongoDb documentation :
Internally, Date objects are stored as a 64 bit integer representing the number of milliseconds since the Unix epoch (Jan 1, 1970), which results in a representable date range of about 290 millions years into the past and future.
https://docs.mongodb.org/manual/reference/method/Date/
You can find some examples on how to insert using the Java driver here
On a read, there is nothing stopping you from converting the date to any format you want.

Parse unix, UTC android

I have JSON as in the picture below which I use in my android application. I have problems with parse of sunrise and sunset which in unix, UTC format. My question is how to show only local hour and minute?
Thats what I tried to sunrise but it was wrong: sunrise.setText(json.getJSONObject("sys").getString("sunrise"));
Thanks for any help!
I believe that you have a couple of issues.
The first is how to convert a Unix timestamp into a Java Date.
Like Unix timestamps, Java dates are stored internally as a long offset from a specific instant, known as the "epoch". Fortunately, they both use the same epoch. However, Unix timestamps have higher resolution.
To get a long that represents a Java date from a Unix timestamp, you need to multiply it by 1000. Then you can use the result to initialize a Java Date.
The essential thing here is to make sure that your timestamps are longs, not ints (or doubles, or any other numeric type).
So, essentially:
long unixTimestamp = 1427607706;
long javaTimestamp = unixTimestamp * 1000L;
Date date = new Date(javaTimestamp);
Next, you'll need to get the numeric value from the Json, as a long, instead of a String, so use getLong instead of getString.
Finally, you'll want to format the date. Since you want just the hours and minutes, you can use:
String sunrise = new SimpleDateFormat("hh:mm").format(date);
and then use that value to set the value of your TextView.

Sqlite Query is not working properly

I have created a database with a String column which contains dates in String format (dd/mm/yyyy). I want to fetch the data from that table which is between two given dates, but when I tried with the below query, I found that it doesn't make any difference what month and year I have selected; it compares the "dd" field only from "dd/mm/yy".
The below query will display all the data which is between day 14 to 25 from every month and year. I want data between the given date, month, and year.
Select * from RunningLog
where CAST(RunDate AS DATETIME) between CAST('14/04/2011' AS DATETIME) and
CAST('25/04/2011' AS DATETIME)
Please see my answer here about how dates are (or are not) stored in sqlite3. Sqlite doesn't have a date field, so its actually stored as a string. Trying to sort / filter on this will prove to be difficult. Instead use an int field, and store the time in milliseconds.
I prefer INTEGER / Unix time storage, then use the built in date and time functions to format when pulling from DB.
Example:
long millis = Calendar.getTimeInMillis();
Store millis in the database as an integer. Then, refer to the first link on how to use the date and time functions in sqlite3.
Sqlite3 documentation does not say it can cast a datetime: http://www.sqlite.org/lang_expr.html (refer to Cast expressions numeral). Why don't you do that from Java? I mean, how are you storing the dates in the database? I highly recommend saving a long (Unix time); that way you can easily get a couple of long numbers that represent an exact date and time and use them to query your table.
Recomendation: Use Datetime fields in the database and JodaTime as a Time library.
Is quite easy to parse the datetime into a DateTime object and then you have many useful methods to compare and work with dates.
Also, your SQL queries will be better.
You can compute the number of seconds between two dates. Here is an example:
SELECT strftime('%s','now') - strftime('%s','2004-01-01 02:34:56');
Based on the sign of the difference you can say if one date is before another. In your case you have to do two comparisons, so you have to verify the sign of two differences. Here you can find other examples, maybe they give you other ideas (http://www.sqlite.org/cvstrac/wiki?p=DateAndTimeFunctions).

How to insert date in sqlite through java

I want to make a database that will hold a date in it(SQLite).
Now first to ask is what is the right syntax to declare a date column.
The second i want to know is how to insert date in it after that.
And the third thing i want to know is how to select dates between, for example to select all rows which contain date between 01/05/2010 and 05/06/2010.
Thank you
Now first to ask is what is the right syntax to declare a date column.
From the SQLite Data Types documentation:
1.2 Date and Time Datatype
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
Take your pick. I'd go for TEXT or INTEGER. The INTEGER will be faster. If you need to store dates past 1970 (e.g. birthdates, etc), then I'd go for TEXT. If you just need to store creationtime/modificationtime, etc for now and the future, then go for INTEGER.
The second i want to know is how to insert date in it after that.
Use PreparedStatement#setString() or #setLong() respectively.
And the third thing i want to know is how to select dates between, for example to select all rows which contain date between 01/05/2010 and 05/06/2010.
Use the standard SQL BETWEEN clause for this. You first need to convert the date accordingly using the built-in date and time functions.
A convenient syntax for declaring a date column is like this:
"CREATE TABLE "
+ "my_table"
+ "(date_time "
+ " DATETIME DEFAULT CURRENT_TIMESTAMP);");
This will default inserted values to the current datetime. Or you can use CURRENT_DATE, or CURRENT_TIME values. You won't have to insert a timestamp manually each time you create a row.
You can read about this approach here: Android insert datetime value in SQLite database

Categories

Resources