In my mongo db database i have column like this.
"created_on" : ISODate("2014-07-02T01:38:48.713Z");
In order to to search this column am giving the following query:
db.XYZ.find({ "created_on" : ISODate("2014-07-02T01:38:48.713Z")})
Now i want to use java for retrieving this data from database:
My query is like:
DateTime dateTime = new DateTime( "2014-07-02T01:38:48.713Z" );
BasicDBObject query1 = new BasicDBObject("created_on", dateTime);
DBCursor cursor = table.find(query);
but am not getting anything query is returning 0 rows??
Any body please help how to set iso date from java.
The mongodb driver does not work with DateTime currently. You have to use java.util.Date
Related
In Data base , createdDt is storing formated like:
15-01-20 10:43:20.394000000 AM
I am passing "created" as dd-mm-yyyy
I want to take the matching date from the table(without comparing time)
#Query("SELECT p FROM ABC p WHERE ( COALESCE(:created) is null or p.createdDt = :created) order by p.createdDt desc")
List<ABC> filterABC(#Param("created") Date created);
How to parse the date within query ?
You could try to use native query using specific DBMS stuff to extract date part.
#Query(value = "SELECT * from ABC where DATE_FORMAT(createdDt, '%d-%m-%Y') = ?1", nativeQuery = true)
List<ABC> filterABC(Date created);
DATE_FORMAT is MySQL specific function. Use the appropriate date function in accordance with your DBMS
My Java application uses a MySQL database that I am trying to convert to SQLite. I was able to convert the database itself just fine, but am having an issue with the timestamps that are stored within the database.
Specifically, my timestamps were stored in the default MySQL format:
2018-04-14 16:33:00
Now, when trying to read the timestamp in SQLite, I am getting the following error:
ParseException: Unparseable date: "2018-04-14 16:33:00" does not match (\p{Nd}++)\Q-\E(\p{Nd}++)\Q-\E(\p{Nd}++)\Q \E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q:\E(\p{Nd}++)\Q.\E(\p{Nd}++)
Is there an efficient way to handle this without actually modifying the format that is stored in the database?
Here is the DDL for one of the tables where this problem exists:
create table agent_list_updates
(
update_id integer not null
primary key
autoincrement,
agent_count integer not null,
update_timestamp timestamp default CURRENT_TIMESTAMP,
user_id integer not null
constraint fk_agent_list_updates_users
references users
)
;
create index idx_agent_list_updates_idx_agent_list_updates_user_id
on agent_list_updates (user_id)
;
The query:
SELECT
agent_count,
update_timestamp
FROM agent_list_updates;
And the Java code that actually causes the error:
if (resultSet.next()) {
return resultSet.getTimestamp("update_timestamp");
} else {
return null;
}
You have to define date format, and then use it to parse your timestamp as a String from resultSet. An example:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while(resultSet.next()) {
Date date = formatter.parse(resultSet.getString("update_timestamp"));
System.out.println(date);
}
because there is no default constructor for Timestamp, or you can do it with the method:
new Timestamp(System.currentTimeMillis());
but Date does not provide any such method directly.
I want to fetch result between start_date to end_date so that i can find my product report.
sql = new StringBuilder()
.append(" select oi.last_modified,oi.product_key")
.append(" from orders o join order_item oi ")
.append(" on oi.id_order = o.id ");
.append(" AND o.created between '")
.append(":start_date") // line - 1
.append("' and '")
.append(":to_date"); // line - 2
in both lines (line-1 & line-2)
I am trying to use prevention technique for that i am using
MapSqlParameterSource namedParams = new MapSqlParameterSource();
so for achieving this i tried lots of combination but no luck.
namedParams.addValue("start_date","start_date",Types.TIMESTAMP);
namedParams.addValue("to_date","to_date",Types.VARCHAR);
I am getting different error for different try.
i.e.
java.util.Date cannot be cast to java.sql.Date
invalid input syntax for type timestamp with time zone
I think it also depends on the data type of your database field. In MySQL for example there are date, datetime and timestamp.
For me having a MySQL datetime and working with a java.util.Date object creating a java.sql.Date and specifying the Types.TIMESTAMP worked (startDate being the java.util.Date object):
MapSqlParameterSource namedParams = new MapSqlParameterSource();
namedParams.addValue("start_date", new java.sql.Date(startDate.getTime()), Types.TIMESTAMP);
I removed single quote and it works for me.
dynamicColumnsQuery = new StringBuilder();
dynamicColumnsQuery.append("SELECT concat(TO_CHAR(months, 'MonYY'),' Adverts') AS monthYear FROM generate_series( CAST(:fromDate");
dynamicColumnsQuery.append(" as DATE) , CAST(:toDate");
dynamicColumnsQuery.append(" as DATE) , '1 month' ) AS months");
I am working on web-services by using java with MongoDB, in that i am trying to check whether a date time say "2015-04-16 16:32:49" lies between the date time present in the fields start_time and end_time from table.In mysql i tried the following query
select * from tablename where Employee_id =101 and '2015-04-16
16:32:49' between start_time and end_time;
i am trying to replicate the same using java in MongoDB by using following code
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("employeeID", "101");
String date = "2015-04-17 11:02:49";
whereQuery.put(date, new BasicDBObject("$gt", "start_time ")
.append("$lt", "end_time "));
DBCursor cursor = col.find(whereQuery);
but the code is not working, it is not comparing the date specified with the two column in table,what is the right syntax to do that
if i try this code
whereQuery.put(someOtherField, new BasicDBObject("$gt", "2015-04-17 11:02:49")
.append("$lt", "2015-04-19 11:02:49"));
it is working i need the syntax to compare a date String with two fields in entire table
Your query roughly translates to:
select * from tablename where Employee_id =101 and '2015-04-16 16:32:49' > "start_time" and '2015-04-16 16:32:49' < "end_time";
You are comparing string "start_time" and "end_time" with '2015-04-16 16:32:49'.
Correct query would be:-
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("employeeID", "101");
String date_string = "2015-04-17 11:02:49";
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
Date date = format.parse(date_string);
whereQuery.put("start_time", new BasicDBObject("$lt", date));
whereQuery.put("end_time", new BasicDBObject("$gt", date));
DBCursor cursor = col.find(whereQuery);
You can use a regular JAVA date object.
Here I am going to get data based on date only but my data continence both date and time here I am using like query to select that data based on date but I am not getting it can any plz exp line it thanks.
String device = "NR09G05635";
String date = "2013-11-29";
java.util.Date temp = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(date);
java.sql.Date date1 = new java.sql.Date(temp.getTime());
sql = "select * from gpsdata1 where deviceId=? and dateTime like '" + date1 + "'";
System.out.println("sql" + sql);
ps1 = con.prepareStatement(sql);
ps1.setMaxRows(1);
ps1.setString(1, device);
ps1.execute();
rs = ps1.getResultSet();
-You use the LIKE operator to compare a character, string, or CLOB value to a pattern. Case is significant. LIKE returns the BOOLEAN value TRUE if the patterns match or FALSE if they do not match
Use TO_CHAR to explicitly create a string based on a DATE, using the format you want. Don't rely on implicit conversions.
Select *
From gpsdata1
Where NVL ( TO_CHAR ( dateTime
, 'YYYY-MM-DD HH:MI:SS AM'
)
, ' ' -- One space
) Like '%';
SELECT * FROM gpsdata1
WHERE deviceId=? and CONVERT(VARCHAR(25), dateTime, 126) LIKE '2013-11-19%'
LIKE operator does not work against DATETIME variables, but you can cast the DATETIME to a VARCHAR