Incorrect Date Exception - java

I am trying to enter a date with year 1900 using java into sql, but I am getting exception as :
com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1900-01-01 00:00:00'

If you are using TIMESTAMP to store the dates the minimal value is '1970-01-01 00:00:01'.
Use DATE to store older dates or DATETIME to store older dates with time.
Check the MySQL documentation about the date format http://dev.mysql.com/doc/refman/5.1/en/datetime.html

Related

How to make sure mongo stores the date as is without converting to UTC? [duplicate]

Using Spring Boot 1.5.4.RELEASE and Mongo driver 3.4.2.
I want to store LocalDate in mongo DB, but I am facing a weird problem:
LocalDate startDate = LocalDate.now();
LocalDate endDate = LocalDate.of(2020,12,01);
System.out.println("---- StartDate : ---"+startDate);
System.out.println("-----End Date : ----"+endDate);
repository.save(new Person("Mehraj","Malik", startDate, endDate));
Output on console:
---- StartDate : ---2017-08-26
-----End Date : ----2020-12-01
But In MongoDb it is storing incorrect dates.
Following is the json from MongoDb:
"startDate" : ISODate("2017-08-25T18:30:00.000Z"),
"endDate" :ISODate("2020-11-30T18:30:00.000Z")
Also, I have noticed that the stored time is also incorrect according to Indian time.
Why the dates are correct on console but not in MongoDB and how to resolve this problem?
The mongo-java client for a date object returns as instance of
java.util.Date.
The problem could possibly be that while you save the startDate and the endDate value, its toString() method would probably use the JVM's default time zone to update the value.
The doc here states that The official BSON specification refers to the BSON Date type as the UTC datetime. and that could be the reason your LocalDateTime attributes were converted to the UTC time zone prior to being saved to the DB.
Also to avoid such confusion would suggest using the bson type timestamp to update date fields.
In the MongoDB Java Driver 3.7 release : http://mongodb.github.io/mongo-java-driver/3.7/whats-new/ we can see that the driver now support LocalDate :
JSR-310 Instant, LocalDate & LocalDateTime support
Support for Instant, LocalDate and LocalDateTime has been added to the driver.

MySQL updating strings in a column to be DateTime

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

insert java date time into Oracle

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"

How to select by date only if field is a timestamp?

I have a database field timestamp without timezone, that has values like 2015-11-23 14:42:55.278.
Now I want to find database records with just using the date part 2015-11-13.
Is that possible?
Ideally using hibernate and spring.
I'm not sure if is the best way in performace terms, but you may search dates between 2015-11-23 00:00:00.000 and 2015-11-23 23:59:59.999
If you want to fetch only for day 2015-11-13 then you can fetch all records using between keyword and by using timestamp of start of day.
dateField between 2015-11-13:<time_of_beginning_of_day> AND 2015-11-14:<time_of_beginning_of_next_day>
or
dateField between 2015-11-13:<time_of_beginning_of_day> AND 2015-11-13:<time_of_end_of_day>
You can cast the column to a date, e.g:
Postgres specific:
the_timestamp_column::date = date '2015-11-13'
or (standard SQL)
cast(the_timestamp_column as date) = date '2015-11-13'
You can also "reduce" the timestamp to different levels using date_trunc()
http://www.postgresql.org/docs/current/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC

Retrieving date from mysql

I have mysql datatype as date, my date format in database is 20/1/2015. Now I wish to retrieve data using date.
For example:
SELECT * FROM Device_Count where Date='20/1/2015'
GO
I am getting error like:
Incorrect date value: '20/1/2015' for column 'Date' at row 1
Please find and post the status
MySQL's date format is yyyy-mm-dd
change your query as below and it should work, provided your Date column data type is date.
SELECT * FROM Device_Count where Date=STR_TO_DATE('20/1/2015', '%d/%m/%Y' )
Refer to Documentation:
STR_TO_DATE(str,format)
Check your date format and compare it by the same format
Date is stored in MYSQL in the format of YYYY-mm-dd. Try this query:
SELECT * FROM Device_Count where Date='2015-01-20'
GO

Categories