Java and MySQL date problem - java

I do a
rs.getTimestamp("datetime")
in Java.
In the database, this datetime is 2009/03/06 02:47:18
but the time is returned as 14:47:18
I'm not very knowledgeable about MySQL dates, so I would appreciate any explanation as to why this is happening.

It doesn't matter. Its not about MySQL or any database. This is the format Timestamp shows up by default, I believe. It doesn't mean it missed the date or something.
You can always format the Timestamp returned by the method in any format in your code. Check out java.text.SimpleDateFormat class. Or for better, check out much more sophisticated Joda Time.

Two things. First, I think we need sample code. What's going on is not at all clear from what you've given us. Context, usage, DB schema, and sample rows as well.
Second, ResultSet.getTimestamp() should be returning an object of type Timestamp, rather than a String of any sort.

SimpleDateFormat time = new SimpleDateFormat("HHmmss");
datime = time.format(rs.getTimestamp("datetime"))
and then datime is printed to a file.
the datetime column in the table is a datetime Data Type

Related

How to fix Cannot parse "DATE" constant issue in H2 Database?

I am porting my Java application which was developed for Windows to AIX Unix. On Windows it uses SQL Server for configuration. On AIX we are trying to use H2 database. Most of the code works but I am getting following error when executing query which has a datetime criteria.
org.h2.jdbc.JdbcSQLDataException: Cannot parse "DATE" constant "26-Jun-2019"; SQL statement:
SELECT EM_SCHEDULER_DAILY_POLL.* FROM EM_SCHEDULER_DAILY_POLL, EM_CONTROLLER WHERE EM_SCHEDULER_DAILY_POLL.CONTROLLER_ID =
EM_CONTROLLER.CONTROLLER_ID AND EM_SCHEDULER_DAILY_POLL.DATE_TIME
BETWEEN '26-Jun-2019' AND '26-Jun-2019 23:59:59' AND
POLLED_SUCCESSFULLY=0 AND EM_SCHEDULER_DAILY_POLL.CONTROLLER_ID=30
[22007-199]
This SQL works perfectly on SQL server but gives above exception on H2DB. How to solve this issue? I need both date and time in query.
Try using ISO date literals:
WHERE
EM_SCHEDULER_DAILY_POLL.DATE_TIME >= '2019-06-26' AND
EM_SCHEDULER_DAILY_POLL.DATE_TIME < '2019-06-27'
Note that since you are just looking for records on a single date, you could also try casting the column to date and doing a single comparison:
WHERE CAST(EM_SCHEDULER_DAILY_POLL.DATE_TIME AS DATE) = '2019-06-26'
As another comment, the first version I gave, with the two inequalities is sargable, meaning that the database should be able to use an index on the DATE_TIME column, while the second version, using the cast to date, probably cannot use an index. Therefore, the first version is the preferred way to go, if you ever need to tune or optimize your database.
You are passing a value with a time but H2 Date only don't have one.
Just remove the time in your second constant.
'26-Jun-2019 23:59:59' --> '26-Jun-2019'
DATE The date data type. The format is yyyy-MM-dd.
Mapped to java.sql.Date, with the time set to 00:00:00 (or to the next
possible time if midnight doesn't exist for the given date and
timezone due to a daylight saving change). java.time.LocalDate is also
supported on Java 8 and later versions.
Example:
DATE
Source :Data type of H2.
And since you just want one day (at least in that example), you can simply use :
DATE_TIME = '26-Jun-2019'
Note that Tim Biegeleisen's answer about ISO should be checked too, this format is not the best
Use TO_DATE function
Example - TO_DATE('01-12-2019','dd-MM-yyyy')
Insert into student(Id,Name,DOB) values(1, 'Abc', TO_DATE('01-12-2019','dd-MM-yyyy'))
There is a converter PARSEDATETIME() fuction.
For example if the date is 12/03/2013 we need to convert as PARSEDATETIME('12/03/2013','dd/MM/yyyy') check this SO.
The SQL statement look like Insert into invoice(id, invoice_date) values(1, PARSEDATETIME('12/03/2013','dd/MM/yyyy'))

assigning value from sql request of type datetime into java.util.Date variable [duplicate]

So far, I have not found a clear answer to this.
I'd like to know what the equivalent is for a SQL type DATETIME and the java type, using a PreparedStatement.
I have found: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
But it states that SQL type "DATETIME" is the same as sql.date, but when looking at the SQL date docs (http://download.oracle.com/javase/7/docs/api/java/sql/Date.html), it says the time is truncated (all zeros).
What I want is to be able to specify a preparedStatement.setDateTime() or some sort.
The only other way I see is using a timestamp, but that would require me to change the column type, while I cannot imagine someone else never had this problem before?
Any hints?
Edit: I am using MYSQL.
The java.sql package has three date/time types:
java.sql.Date - A date only (no time part)
java.sql.Time - A time only (no date part)
java.sql.Timestamp - Both date and time
You want the last one: java.sql.Timestamp.
If you are using these types, you don't need to call a specific setter; just use:
java.util.Date date = new Date();
Object param = new java.sql.Timestamp(date.getTime());
// The JDBC driver knows what to do with a java.sql type:
preparedStatement.setObject(param);
The equivalent of MS SQL Server or MySQL DATETIME data type or Oracle DATE data type is java.sql.Timestamp.
In Java we have java.util.Date to handle both Date and Time values.
In SQL, you have commonly Dates (only dates), Time (only time) and DateTime/Timestamp (date and time).
In your Java program, usually you'll always have java.util.Date, so each time you're setting Dates/Times/DateTimes in PreparedStatements, always choose exactly which one you need, according to the database.
I had a similar problem with my Mysql having SQL date and locally in my app i only had Date
I solved like this
java.sql.Date dataStartSql = new java.sql.Date(start.getTime());
After that used the setDate normally, and I used a getTimestamp to retrive the first value.
where start is a Date object.

Oracle truncates the time part of date

In my Java app I must save data to Oracle 11g with object created date and time and for this I convert java.util.Date() to java.sql.Date() in format as new java.sql.Date(new java.util.Date().getTime()). But I noticed, that after the data inserted, oracle truncate the time part of date and I get something like 16/09/2015. but I need format like this: 16/09/2015 9:55:44. the second format created by oracle's sysdate() procedure. How I can get the second format from java code?
From memory (it's been a while), java.sql.Date is used to hold dates (no time) only, if you want time information as well, you need to use java.sql.TimeStamp instead.
Do not use java.sql.Date if you want to store date and time. Just use java.util.Date without any conversion. Simple and easy. There is no need for java.sql.TimeStamp either.
And make sure your NLS settings (e.g. in SQL Developer) are such that they display both date and time as Oracle does not distinguish between dates with and without time.
I guess Oracle does not truncate. For example, if you use Oracle SQL Developer, you have to update NLS parameter "Date Format" to see a time.
By default it just equal "DD-MON-RR"

update ACA_CHECK_HISTORY set Datecolumn=CONVERT(VARCHAR(15),getdate(),103)

I am using mysql server 5.X
update ACA_CHECK_HISTORY set Datecolumn=CONVERT(VARCHAR(15),getdate(),103);
I am getting syntax error.
Try using MySQL syntax and not SQL Server syntax:
update ACA_CHECK_HISTORY
set CREATED_DATE = date_format(now(), '%Y-%m-%d');
Oh, wait, that's not quite what you want. It is better than what you want.
First, you should store date/time values in the native format in the database. If, for some reason, you cannot do this, then use an ISO standard format -- such as YYYY-MM-DD. This works for sorting and comparisons, which makes it very useful.
You can convert the value to an output format when presenting it to users. You seem to want the format '%d/%m/%Y'. Nothing wrong with that format. It is just a presentation format and should not be stored in the data.
If that column is a DATE or DATETIME or TIMESTAMP datatype, then simply do
update ACA_CHECK_HISTORY
set CREATED_DATE = NOW();
Better yet, you can avoid the UPDATE entirely if you use a TIMESTAMP with suitable DEFAULT.

Why date is inserted as 02/10/0010 instead of 02/10/2010

I am inserting a record to orcle db through java application. The date value inserted as 02/10/0010 instead of 02/10/2010 HH:MM:SS AM/PM? I am using oracle jdbc connection. Does it problem with JDBC driver ?
Any input on this.
If you're using Oracle and JDBC, don't store Date in your table as a String. Make it a real Date and you'll spare yourself all this pain.
Check your date source. Is it two digit year or four?
Also, there are Oracle date mask formats which might cause that. Check the default for the installation.
Agree.
If you use a Java Date format (dunno its class just now) then JDBC driver performs all what is needed to store it in a Date column.
If you use String in java and/or varchar2 in Oracle table, you are doing it wrong. This will lead to implicit conversions, NLS settings and all that pain... if you are in this bad shape then you need explicit conversions and format masks on each date usage.
Use proper types.
I'm guessing that the value you're passing in is "02/10/10".
Date functions are pretty inconsistent about what they do with 2 digit years. If you are trying to enter dates for your "timeline of ancient history", having the computer assume that 2-digit dates must be shorthand for the 21st century would be very annoying. We really are better off with a WYSIWYG date interpretation.

Categories