Converting java.util.Date to Java.sql.Date using Javalin - java

I am creating a web app using Java (Javalin and Maven) for university.
We have to make a website for movie bookings and I am struggling to find out how to properly convert dates so that they are readable by SQL and Java.
I have to store date values in a database and to this point I have just been storing them as a string but I want it to have a more specific date meaning. It is built using the MVC model.
This is the code in my sessions model.
public void setSessionDate(Date sessionDate) {
java.sql.Date date = new java.sql.Date(sessionDate.getTime() );
this.sessionDate = sessionDate;
That is the code in my SessionsDao file.
stm.setDate(3, new java.sql.Date(sessions.getSessionDate().getTime()));
And finally this is the code in my SQL create field.
sessionDate DATE not null,
When I try to create a new movie session this is the error the console prints.
[qtp198903030-30] WARN io.javalin.Javalin - Uncaught exception
io.javalin.core.validation.MissingConverterException: Can't convert to Date. Register a converter using JavalinValidation#register.
at io.javalin.core.validation.Validator$Companion.create(Validator.kt:35)
I am unsure how to convert the date properties correctly.
Any help or other methods on how to proceed would be greatly helpful!

Related

How to maintain client side time and server side time in synch in a spring boot project?

Currently I am facing an issue with datetime in my spring boot project:
I am getting data from UI as:
projectStartDate: "2020-07-15"
When I am saving the data in DB, it's getting stored as 2020-07-14. My Microsoft SQL Server is in USA and i am accessing the application from India. Here is my code:
Entity class:
import java.util.Date
#Column(name = "project_start_date")
private Date projectStartDate;
Pojo Class:
import java.util.Date;
private Date projectStartDate;
And this is what I am doing in my service:
dataToSave.setProjectStartDate(projectMasterData.getProjectStartDate());
The problem is when I am entering date 2020-07-15 from UI, but in db it's getting saved as 2020-07-14.
I want to make it as if someone accessing the application from australia, the date should get saved as it's entered in UI like if I entered the date as '2020-07-15', it should save a '2020-07-15', if someone else from USA is accessing the application and trying to get the data of the saved object it should show the USA date of '2020-07-15'.
Can someone help me here please. I am stuck here for hours. Thank you

How to get the current date running on my MongoDB instance using spring data or pure java

Is that anyway to get the current date from my MongoDB database server, using spring data or pure java?
You can run the $eval command to get the current date by calling javascript like new Date(), new ISODate(), ISODate. More here https://docs.mongodb.com/v3.2/reference/method/Date/
Document doc = db.runCommand(new Document("$eval", "new Date()"));
Date current = (Date) doc.get("retval");

Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date

I am getting the below given error for the following code snippets:
try {
cRows = new CachedRowSetImpl();
while(cRows.next())
{
MyClass myClass = new MyClass();
myClass.setPrevDate(cRows.getDate("PREV_DATE")); // In debug mode, the error was throwing when I press Resume from here.
}
}
Error:
Caused by: java.lang.ClassCastException: java.sql.Timestamp cannot be cast to java.sql.Date
In the database, the datatype for the column is DATE only. I am not able to figure out where the Timestamp is coming here.
Obsolete:
Use java.util.Date for the field. java.sql.Timestamp is a direct subclass of it. As is java.sql.Date - that strips the time part. Why the java database driver takes DATE to be Timestamp is a bit weird. What is the database vendor? Did you specify a length or so? Are indeed only dates stored?
Researched:
I looked into CachedRowSetImpl.java, and Oracle's docs and Oracle does everything fine (java.sql.Date, java.sql.Time, java.sql.Timestamp convertible).
The CachedRowSetImpl does simply cast the DATE's Object (and getObject is likely to return the high resolution Timestamp - with time) to java.sql.Date, and that's wrong.
So override or substitute this sun's class.
/*
* The object coming back from the db could be
* a date, a timestamp, or a char field variety.
* If it's a date type return it, a timestamp
* we turn into a long and then into a date,
* char strings we try to parse. Yuck.
*/
switch (RowSetMD.getColumnType(columnIndex)) {
case java.sql.Types.DATE: {
long sec = ((java.sql.Date)value).getTime();
return new java.sql.Date(sec);
}
I have done a research on this issue and found some useful links. I found this confusion between DATE and TIMESTAMP is JDBC Driver specific. And most of the links suggest the use of -Doracle.jdbc.V8Compatible=true. For my JBoss I have set this in run.bat and the issue got resolved.
https://community.oracle.com/thread/68918?start=0&tstart=0
http://www.coderanch.com/t/90891/JBoss/oracle-jdbc-Compatible-true
https://community.oracle.com/message/3613155
The oracle doc shares different solutions:
Alter your tables to use TIMESTAMP instead of DATE. This is probably
rarely possible, but it is the best solution when it is.
Alter your application to use defineColumnType to define the columns
as TIMESTAMP rather than DATE. There are problems with this because
you really don't want to use defineColumnType unless you have to (see
What is defineColumnType and when should I use it? ).
Alter you application to use getTimestamp rather than getObject. This
is a good solution when possible, however many applications contain
generic code that relies on getObject, so it isn't always possible.
Set the V8Compatible connection property. This tells the JDBC drivers
to use the old mapping rather than the new one. You can set this flag
either as a connection property or a system property. You set the
connection property by adding it to the java.util.Properties object
passed to DriverManager.getConnection or to
OracleDataSource.setConnectionProperties. You set the system property
by including a -D option in your java command line.
java -Doracle.jdbc.V8Compatible="true" MyApp
Here is the link: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_00

HIBERNATE : Don't want to save milliseconds

I got a little problem and I didn't find a suitable solution on the net because my question is a bit tricky for search engine.
There's a lot of topics about hibernate saving milliseconds. But my problem is something else.
In fact, I got a database, which save my date like this :
2014-03-20 10:58:09
I used Hibernate to get back my date, and display it on a web page. But Hibernate retrieve more than that : it also retrieve a 0 milliseconds, like this :
2014-03-20 10:58:09.0
Many people seems to have problem with this, but in my case, I DON'T WANT this information, I want Hibernate to retrieve the date without this .0 !
Thanks for your help !
EDIT AND SOLUTION :
Ok so I made it by using a little hack.
In my specific object using by Hibernate, I had this method :
public Date getModificationDate() {
return modificationDate;
}
I just simply create an other method :
private static final SimpleDateFormat FMT = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public String getModificationDateLabel() {
if (modificationDate != null) {
return FMT.format(modificationDate);
}
return null;
}
So, when I display in my webpage (I use Velocity Template), I just run through my list of object an display the label :
#foreach( $object in $objects)
$!{object.modificationDateLabel}
#end
The SimpleDateFormat allow me to remove the .0, and by creating a new method, I don't disturb the behavior of getting a Date with Hibernate.
Thanks for your time !
I don't see a problem with the date returned as "2014-03-20 10:58:09.0" is equal to "2014-03-20 10:58:09". Can you provide specific scenario where this can result in issue?
Or use SimpleDateFormat("yyyy-MM-dd HH:mm:ss") then parse your date in this format before using the date.

Trouble when inserting time and date to db using Java

Here's my code:
uprs.moveToInsertRow();
uprs.updateString("Sender", myName);
uprs.updateString("Receiver", withWho);
uprs.updateString("Message", myMessage);
//uprs.updateString("Time",****);
uprs.insertRow();
Things go perfectly when adding the non-time data into the database.
But I am struggling how to add the current date and time into the database.(Ms SQL 2008)
Is there any expert can tell me how to organize the code about getting the current time and date and insert them into the db?
The data type of Time in my db is "datetime".
Thank you!
For a date/time type you should use updateTimestamp instead of updateString

Categories

Resources