Using a criteria for time restriction - java

I'm having some problems with a simple query I'm doing.
I have a postgresql database, with a time wihout zone column.
I cannot change the type of this column. Also, I have to use criterias, so don't tell me to change this. However, if there is a better solution for the future instead of using the time type I'm curious about it.
This column is mapped to a java.util.Date attribute. This could be changed to Time or whatever.
What I'm doing is adding this :
Time someTime = someDate.getTime();
criteria.add(Restrictions.eq(propertyName, someTime)));
This criteria doesn't return anything, when it should. What is done is the following: The user puts some string time, that is parsed to Date, being that 1970-1-1 and the time. This time is then added to the criteria. Also, the time in the database I'm using as example, is the same Date (1970-1-1, etc) that the one that is created when parsing the user data.
I have tried to search for some documentation on how criterias are used against time without timezone but haven't found anything.
Suggestions why this is failing?
UPDATE:The problem has been solved using the library JodaTime. But I still don't understand why was failing...

Related

Creating date/time/location class and storing it in mysql

So my goal is to create a class where someone can log in and create an 'event', which really just means they can store a String that is the name of the 'event' (like Woodstock), a future date, and a time that the event starts. I have to store this in a mysql database (I am using LAMP). I've found Joda time, but I have no idea if LocalDateTime can be stored easily in my database (I don't necessarily need time zones as this is a local program but if it needs to have time zones thats fine).
What would also be cool, but I really don't have to implement this but if they could see a calendar for like the next three months or something so they can add the date easily. Anyway, does anyone recommend one format over any others?

How to GET last dates within a certain constraint

I am trying to figure out how I can pull records from a mongo database using Jongo that will check the date that is on the object in the db and will only return an object with the data from, in my case, the last three most current dates. I'm using LocalDate to set the date on the object.
So that's the problem, what I'm doing is I have an angularjs web ui that interfaces with a java backend and I'm trying to scale down my scope, because the current way I'm displaying data is not feasibly scalable. So I want to limit the response I receive to the last 3 objects created instead of all of the objects. And I'm to the point where I need to do a check on the date but I can't seem to find a reasonable solution. isAfter() and isBefore() was where I started my initial effort, but I can't guarantee the dates in any way so I don't know where to set my constraints.
The code I'm working on doesn't really matter, I'm just more interested in how I can do the check in general.
Thanks in advance for any help, if any at all, can be offered!

Converting date as timestamp while selecting using Hibernate APIs

We have a GUI application which sends around 40 parameters to the DB.
Based on these parameters a dynamic query is generated to fetch data. Number of fields that needs to be selected remains the same, only where condition differs.
Hibernate criteria API is used to form dynamic queries. We are using SQLMX as the database running on HP Nonstop server.
Certain dates need to be stored as 0001-01-01. Now, while selecting this field (DATE datatype), application displays this as 2001-01-01. When query is run from RazorSQL also, we are seeing incorrect results.
I could resolve this issue in RazorSQL by doing "select (cast(date1 as timestamp) from table". This gives date correctly, but with timestamp. This is OK.
In Java application, I suggested project team to use NamedNativeQueries wherein CAST as TIMESTAMP construct is specified in the query itself. Later on , I came to know that team cannot used NamedNativeQueries since queries are dynamic in nature. (Where condition can have 1 or 5 or no clauses).
Tried using Temporal.DATE annotation. Did not work. What i observed is that, we need to cast the date to timestamp while selecting itself. setMethod for the variable gets called after query has happened and if we do not do cast while doing select, value of 0001-01-01 gets changed to 2001-01-01.
Is there any way we can apply SQL constructs (similar to_date in Oracle ) before the query is executed?

Date is changed after persist in database?

User inputs date from JSP page, and it converts to Joda DateTime, the string output is
2014-03-26T00:00:00.000+09:00
However when I persist this entity containing date filed in database, and retrieve and print out again, it becomes
2014-03-25T09:00:00.000+09:00.
I don't know why database make this change to minus one day.
I use postgres, hibernate JPA for application development.
Thanks in advance.
What is the value in the database? Use pgAdmin app, the psql command line tool, or some other database admin tool to query Postgres directly.
What data type are you using in Postgres? You probably should be using TIMESTAMP WITH TIME ZONE. Avoid using the WITHOUT time zone type as it ignores any time zone offset info you may provide.
Despite the name, neither type stores any time zone info. The difference is whether you want Postgres to pay any attention to time zone info on incoming data. Be sure to read the doc thoroughly and play with it to experiment until understand how date-time works.
Read Always Use TIMESTAMP WITH TIME ZONE by David E. Wheeler, a Postgres expert.

How to configure correct timezone in JDBC?

I've this url to set up the connection in my Italy website, however, when i try to perform some insert action from the site, the date is still not right. (it should be for example: 01:24, but it is 02:24)
jdbc.url=jdbc:mysql://sql.example.com/database?autoReconnect=true&characterEncoding=UTF-8&sessionVariables=time_zone='Europe/Rome'
Do I need to add any other params to make it work correctly?
Is there a complete list of all timezones?
Sorry I don't have the answer to your direct question. However I can suggest something worth considering that will avoid all time zone problems at the database entirely. If possible I recommend simply using BIGINT fields for storing dates with Java. You just store the long of the number of milliseconds since the epoch, e.g. from System.currentTimeMillis() or Date.getTime().
Then interpretation of the time zone for a date is always managed in Java, which is good at using the epoch based number. It does make it a little more involved to directly query the database for a date outside of Java, however it's not too hard and tends to be worth it IMO:
SELECT FROM_UNIXTIME(date_field / 1000) FROM table;
There is a list of "tz" timezone names in Wikipedia.

Categories

Resources