Oracle date format on server is MM/DD/YYYY HH24:Mi:SS
I would like to insert a variable which contains a date with timestamp into Oracle date column.
I am getting error while inserting date into Oracle "date column ends before format picture ends".
All I want is append specific timestamp to java string date and insert that string/date format into Oracle database
example:
String incoming_date = request.getParameter("insert_date"); //this comes as a string in dd-mon-yyyy format
formatted_incoming_date = incoming_date + " 00:00:01"; //I want to append time factor to above variable with 00:00:01
insert into testtable values(formatted_incoming_date);
Try this
insert into testtable values(TO_DATE (formatted_incoming_date, 'dd-mon-yyyy hh24:mi:ss);
Why you try to insert date as a string? It seems like there is an implicit conversion from string to date in Oracle. Can java.sql.Date be used instead?
Anyway, as long as date comes in format dd-mon-yyyy you have to convert it either into java.sql.Date object or in proper for Oracle string representation as MM/DD/YYYY HH24:Mi:SS i.e.
incoming date "05-12-2016" string for Oracle "12/05/2016 00:00:00"
Related
I have an oracle database table with a date column. when I query the table date is displayed in dd-MMM-yy format. For example 17-OCT-19 format. But when I query the table from java and print the value I am seeing the date in yyyy-MM-dd format. Is there a way where I can fetch the date the same as that is displayed in dd-MMM-yy format? I have no idea why the date is getting displayed in a different format in java.
DateFormat activityDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date activityDate = activityDateFormat.parse();
activityDateFormat = new SimpleDateFormat("dd-MMM-yy");
finalActivityDate = activityDateFormat.format(activityDate);
I am using the piece of code to convert the date from yyyy-MM-dd to dd-MMM-yy format. The NLS settings for Date format in the database are DD-MON-RR. Is there any reason why I don't get the date the same as DD-MON-RR? can someone please help me?
Select the date column as string and format it dd-mmm-yy from the SQL query.
The client you are using to query Oracle, SQL Developer is often used, is the one formatting the date.
Java gets the date as a date data type and will use whatever is the default format when converting it to a string
I am saving a date formatted with Javas SimpleDateFormat with the format "E. MMM-dd-YYYY hh:mm a zzz" eg "Sat. Apr-15-2017 10:44 PM EST" to a table as a VARCHAR.
How would I go about changing all of the pre-existing data to be in DateTime format?
Have you tried this?
UPDATE `table`
SET `column` = STR_TO_DATE(`column`,'%Y-%m-%d')
refer to change mysql comumn from varchar to datetime and convert data post
I read a mysql date time field into one string e.g.
String arriveTime = rs1.getString("arriveTime");
Next step I try to get the current date and time using java to be similar format like the one I got from mysql.
DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTimer=null;
Date date = Calendar.getInstance().getTime();
currentDateTimer=outDf.format(date);
How can I minus the currentDateTime and arriveTime to get the net results in seconds. I would prefer to do it purely via java
First, I would not read a MySQL date-time into a String. I would change this,
String arriveTime = rs1.getString("arriveTime");
to
java.sql.Date arriveTime = rs1.getDate("arriveTime");
Then you can use basic subtraction to get the result in milliseconds, then divide by a thousand to get that in seconds - so
long diff = new java.util.Date().getTime() - arriveTime.getTime();
System.out.println(diff / 1000);
I read a mysql date time field into one string
If the column is varchar type it is ok you can read it using resultsetObject.getString()
But if your column type is Date then is it always recommended to get the value using resultset.getDate()
Mysql stores date in the format yyyy-MM-dd
I try to get the current date and time using java to be similar format
like the one I got from mysql.
When you do resultset.getDate() it will give you the java.sql.Date in format yyyy-MM-dd
I am trying to insert into a variable in MS- SQL database the current date and the time.
I use this format:
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Calendar cal = Calendar.getInstance();
System.out.println(dateFormat.format(cal.getTime()));
and I get this as a result 2013-01-28 09:29:37.941
My field in the database is defined datetime and as I have seen in other tables which have the same field, the date and the time is written exactly like this 2011-07-05 14:18:33.000.
I try to insert into the database with a query that I do inside a java program, but I get this error
SQL Exception: State : S0003 Message: The conversion of a varchar
data type to a datetime data type of the value is out of range. Error
: 242
My query is like that:
query = "INSERT INTO Companies CreatedOn"+
"VALUES ('" + dateFormat.format(cal.getTime()) + "')"
but I don't understand what I am doing wrong.
According to the error description, you are inserting an incorrect type into the database. See JDBC to MSSQL. You should convert Calendar to Timestamp.
Try using:
PrepareStatement statement
= connection.prepareStatement("INSERT INTO Companies CreatedOn VALUES(?)");
java.sql.Timestamp timestamp = new java.sql.Timestamp(cal.getTimeInMillis());
statement.setTimestamp(1, timstamp);
int insertedRecordsCount = statement.executeUpdate();
First of all, do NOT use string concatenation. Have you ever heart about SQL injection?
Correct way how to do that is to use prepared statement:
Idea is you define statement with placeholders and than you define value for those placeholders.
See #Taky's answer for more details.
dateFormat#format this method returns formatted string not Date object. Database field is DateTime and it is expecting java.sql.Timestamp to be inserted there not String according to docs.
To conform with the definition of SQL DATE, the millisecond values
wrapped by a java.sql.Date instance must be 'normalized' by setting
the hours, minutes, seconds, and milliseconds to zero in the
particular time zone with which the instance is associated.
Try java.sql.Timestamp object instead of String in query and I'd recommend you to use PreparedStatement.
This is because you are trying to save String date value to Date type DB field.
convert it to Data dataType
You can also use the datetime "unseparated" format yyyymmdd hh:mm:ss
You could use Joda framework to work with date/time.
It maps own date/time types to Hibernate/SQL types without problem.
When you set parameters in HQL query joda carries about right type mapping.
If you want to store current date and time then you should use MYSQL inbuilt method NOW().
for brief documentation refer http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html . so your code will be like.
INSERT INTO Companies CreatedOn VALUES(NOW())"
However If you want to do it using java Date-util then it should be
Calendar cal = Calendar.getInstance();
java.sql.Timestamp timestamp = new Timestamp(cal.getTimeInMillis());
HI i have a application connecting to the MS access database.
the date is passed as a string from the jsp .
i am converting the string to date as follows
strtDate is the date in string format (eg 4/18/2011 in MM/dd/yyyy format)
DateFormat convDate = new SimpleDateFormat("MM/dd/yyyy");
java.util.Date conDate = (Date) convDate.parse(strtDate);
java.sql.Date convSqlDate = new java.sql.Date(conDate.getTime());
the convSqlDate value am getting as 2011-04-18.
In MS access the data type of date is Date/Time.
how to convert this date to MM/dd/yyyy format.
The formatting doesn't matter once you have a Date object to insert into the database. The JDBC driver will take care of all the details to ensure that your object is stored properly in MS SQL Server.
Formatting matters when you render the Date, but you should have no problems once you've converted.