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
Related
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!
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());
...
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.
I have
#PUT
#Path("{id}")
public Response modify(#PathParam("id") Integer id,
#QueryParam("user") String user, #QueryParam("time") Date time) {....
I am trying to use RestClient to call this web service (the above is actually a cut down version of what I have)
When I call
..../123?user=user1
I hit the web service. As soon as I add time I get a 403 Forbidden message
..../123?user=user1&time=2013-09-10T20:00:00Z
Even if I pass nothing into the time query parameter I get the 403.
Is there anything difference about passing the java dates?
Thank in advance
It's not able to deserialize the String to a Date. Two options are either you can modify the date string as accepted by the date class or use another form, such as a long value.
One observation: It seems you are adding an extra slash(/) before your query params:
change this
..../123/?user=user1&time=2013-09-10T20:00:00Z
to
..../123?user=user1&time=2013-09-10T20:00:00Z
Second thing is that you may have to encode your URL to send the date properly to server
I'm running into a weird problem that doesn't seem to happen all the time, but when it does, it looks like this:
Error: on line 60, column 31 in foo/bar.ftl
Expecting a date here, found: 2011-12-29 04:37AM
The problematic instruction:
----------
==> assignment: createDate=project.createdTime?datetime("yyyy-MM-dd hh:mma") [on line 60, column 9 in foo/bar.ftl]
----------
Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Error: on line 60, column 31 in foo/bar.ftl
Expecting a date here, found: 2011-12-29 04:37AM
at freemarker.core.BuiltIn$dateBI$DateParser.parse(BuiltIn.java:334)
at freemarker.core.BuiltIn$dateBI$DateParser.get(BuiltIn.java:305)
at freemarker.core.BuiltIn$dateBI$DateParser.exec(BuiltIn.java:316)
at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:93)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.Assignment.accept(Assignment.java:90)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:210)
at freemarker.core.Environment.process(Environment.java:190)
at freemarker.template.Template.process(Template.java:237)
at org.springframework.web.servlet.view.freemarker.FreeMarkerView.processTemplate(FreeMarkerView.java:366)
at org.springframework.web.servlet.view.freemarker.FreeMarkerView.doRender(FreeMarkerView.java:283)
at org.springframework.web.servlet.view.freemarker.FreeMarkerView.renderMergedTemplateModel(FreeMarkerView.java:233)
at org.springframework.web.servlet.view.AbstractTemplateView.renderMergedOutputModel(AbstractTemplateView.java:167)
at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
...
The line in bar.ftl where it's failing looks like this:
<#assign createDate = project.createdTime?datetime("yyyy-MM-dd hh:mma")>
The getter in the Project.java code looks like this:
private Date createdTime;
...
public String getCreatedTime() {
SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd hh:mma");
return createdTime == null ? null : sm.format(createdTime);
}
So to me, it looks like everything is getting returned correctly, and that the code is setup correctly. I'm using FreeMarker 2.3.16 and Spring MVC. It seems to work most of the time, but fails sometimes...
Ideas?
The error message means that, internally, DateFormat.parse has thrown java.text.ParseException. Since the string given in the exception message should be parsed OK with that pattern (and I hope your example is exact and real), and since it happens only sometimes, it stinks like a concurrency (multi-threading) issue around the date-format cache. If you could construct a standalone test that demonstrates this, that would make finding it quicker (I'm in charge of maintaining FM, pretty much). Quickly looking at the source code nothing jumps at me... the syncs are there.
Edit: Another possible reason is that sometimes the FreeMarker "locale" setting is not English. The "AM"/"PM" postfix is different for non-English, and so it causes this error.
Your code looks valid, I've no idea why it fails - especially "sometimes".
One thing that strikes me as odd, however, is that you're converting your date to string first (inside getCreatedTime()) only to parse it back within FreeMarker template. Why not pass it as date to begin with? Something like:
// Project.java
public Date getCreatedTime() {
return this.createdTime;
}
// bar.ftl
<#assign createDate = project.createdTime?datetime>