HIBERNATE : Don't want to save milliseconds - java

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.

Related

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

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!

How to get Date AND Time from Oracle database, using Hibernate?

I have been given some code to work on. I need to modify the existing code to return an extra column. Using the tool, SQLDeveloper, I can see an example record (notice Date AND Time information is present):
30-NOV-17 15:54:00
The code that I have been given to work on does the following:
// Create a Hibernate query (Oracle SQL query)
Query query = session.createSQLQuery(<NATIVE_SQL_QUERY_HERE>);
List<Object[]> rawDataRows = query.list();
if (rawDataRows != null) {
for(Object[] rawDataRow : rawDataRows) {
// I am trying to get the Date AND Time here
Timestamp ts = (Timestamp) rawDataRow[7];
}
}
The problem is that I get an error when I try this approach (Cannot cast java.sql.Date to Timestamp).
When I access the data without the cast (just get the data in a Date object), I DO NOT get the Time information. And I need to have BOTH.
So far, nothing I have tried has worked - other posts have similar issues, but they are not quite the same.
Any advice/suggestions much appreciated - THANKS!
You can use this code:
....
Calendar calendar = Calendar.getInstance();
calendar.setTime(rawDataRow[7]);
Timestamp ts = new Timestamp(calendar.getTimeInMillis());
...

Calculate difference between 2 times in MongoDB

I have a field called Last Modified At with value like '2016/04/12 20:24:18'. It is not an ISO Date but a normal String value stored via a java process in MongoDb.
I am trying to write a shell script to calculate the difference between '2016/04/12 20:24:18' and say '2016/04/12 16:24:18'. The difference could be either in days or hours or mins or secs. I tried couple of things including converting to ISO dates but it doesnt work out. Is there an easy way to find out like Oracle.
Any help would be appreciated?
Thanks,
Ram
I'm not exactly sure how you plan on running the shell script, but it is possible in the mongo shell to parse dates (using Javascript) and calculate time between two dates as you have asked. Assume we have a document in the things database as follows:
{
"_id" : ObjectId("570f1e528163383227ace761"),
"lastModifiedAt" : "2016/04/12 20:24:18"
}
We can run the following script to get the difference between the lastModifiedDate of a document and a hard-coded date, such as 2016/04/12 16:24:18:
db.things.find({}).forEach(function(thing) {
var date1 = new Date(thing.lastModifiedAt);
var date2 = new Date('2016/04/12 16:24:18');
var dateDiff = date1.getTime() - date2.getTime();
printjson({_id:thing._id,lastModifiedAt:thing.lastModifiedAt,dateDiff:dateDiff});
});
This results in:
{
"_id" : ObjectId("570f1e528163383227ace761"),
"lastModifiedAt" : "2016/04/12 20:24:18",
"dateDiff" : 14400000
}
where dateDiff is milliseconds and 14400000 milliseconds = 4 hours.
If you provide more information on how you plan on making this call, and where the second date is coming from I would be happy to expand upon this answer.

Play Framework 2.2.3 : Define a default date format for data binding

I have searched for a while but I didn't find anything in the play documentation :
I have a class customer that contains a date :
private Date birthday;
My controller creates the object by binding the request params :
Form<Customer> userForm = form(Customer.class);
Customer customer = userForm.bindFromRequest().get();
At runtime, I get this error :
Caused by: java.lang.IllegalStateException: No value
at play.libs.F$None.get(F.java:702) ~[play_2.10-2.2.3.jar:2.2.3]
at play.data.Form.get(Form.java:540) ~[play-java_2.10-2.2.3.jar:2.2.3]
I figured out that the problem comes from my date format that is submitted with this pattern : dd/MM/yyyy.
I was not able to find how to configure play to set this pattern for french users.
The only thing I found and that works is to use this annotation :
#Formats.DateTime(pattern="dd/MM/yyyy")
private Date birthday;
I'm not satisfied because I don't want to put this annotation everywhere : the date format depends on the user local and should not be a const.
Moreover the play2 documentation does not explain what is the default pattern if nothing is specified...
I'd like to have a conf like that :
date.format.en = MM/dd/yyyy
date.format.fr = dd/MM/yyy
Do you know if it is possible with play2 ?
Any help would be appreciated :D

Spring #RequestParam vs #ModelArribute when getting a java.util.Date

I have a situation where the following code correctly gets the Date, without the time:
#RequestMapping(value="/pdf", method={RequestMethod.GET,RequestMethod.POST})
public void printPdf(#RequestParam("printDateTime") Date printDateTime .... more) {
printDateTime; // contains date, but NOT the time
...more code here...
}
However, when I use modelAttribute, it will correctly get the date AND the time from the client.
#RequestMapping(value="/pdf", method={RequestMethod.GET,RequestMethod.POST})
public void printPdf(#ModelAttribute WorkerSearchParamsDto searchParamsDto .... more) {
searchParamsDto.getPrintDateTime(); // Returns correct date with client time...
...more code here...
}
I am a little confused about why that would be the case and what I could do to resolve this. There are reasons we would like to not use modelAttribute for the print date, be we can work around it. Any ideas would be helpful.
To be clear, we WANT the time, not just the Date
We are running Spring 3.2.4.RELEASE

Categories

Resources