I need to store a Java Date object in Redis. I'm using Jedis as my Redis client. How do I store a Date object in Redis and retrieve it using Java? Everything I see uses String and Integer values. However, I don't understand enough about this world to do this with a Date object.
How about converting the Date object to Epoch timestamp and converting it back to Date with any date format? This way you will not get into trouble with formatting errors..
//...convert date to epoch timestamp
Long time = new Date().getTime();
//...serialize it to a json object
job.addProperty("dateTime", time);
//...write it to redis
jedis.hset(KEY, field, job.toString());
//...retrieve the field and convert date with any format
model = gson.fromJson(jedis.hget(KEY, field), ModelClass.class);
//...print the date (suppose that the time field is of type Long)
DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println(format.format(model.getTime()));
Hope this helps.
You need to find a way to serialize the data and parse it later. Redis will only store strings, and Jedis doesn't provide serializers. This conversation has more information about your use case.
Related
I read a mysql date time field into one string e.g.
String arriveTime = rs1.getString("arriveTime");
Next step I try to get the current date and time using java to be similar format like the one I got from mysql.
DateFormat outDf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentDateTimer=null;
Date date = Calendar.getInstance().getTime();
currentDateTimer=outDf.format(date);
How can I minus the currentDateTime and arriveTime to get the net results in seconds. I would prefer to do it purely via java
First, I would not read a MySQL date-time into a String. I would change this,
String arriveTime = rs1.getString("arriveTime");
to
java.sql.Date arriveTime = rs1.getDate("arriveTime");
Then you can use basic subtraction to get the result in milliseconds, then divide by a thousand to get that in seconds - so
long diff = new java.util.Date().getTime() - arriveTime.getTime();
System.out.println(diff / 1000);
I read a mysql date time field into one string
If the column is varchar type it is ok you can read it using resultsetObject.getString()
But if your column type is Date then is it always recommended to get the value using resultset.getDate()
Mysql stores date in the format yyyy-MM-dd
I try to get the current date and time using java to be similar format
like the one I got from mysql.
When you do resultset.getDate() it will give you the java.sql.Date in format yyyy-MM-dd
I have a one rest method from Spring3 which returns expected JSON response except date which is in timestamp.
In database date is stored as 2013-08-08 00:30:00. Before sending response to the application, the date I am getting as 2013-08-08 00:30:00. I have checked it in debug mode. But after complete execution of rest-service, I got the date in long format as 1375902000000.
I want to return same timestamp format instead of long format.
I don't want to convert long timestamp to again date format at client side.
By default it is serialized as long if you want to send it like 2013-08-08 00:30:00 you may try asString()
You could do the following to keep your desire date format:
Declare the bean property as String.
Format the date object according to your desire format, example yyyy-dd-MM hh:mm:ss
I am trying to upload the data from a delimited text file to the lotus notes form using java agent. The issue arises when I try to insert the date value to the notes document. After insert when i use ComputeWithForm, then it returns false. I am using simpledateformat to format the date in MM/dd/yyyy format, but it is still not working. Below is the excerpt from my code.
String delim, key, thekey, myDate;
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy);
myDate = dateFormat.format(date);
newdoc.replaceItemValue("UploadDBDate", myDate);
Any help will be great.
Thanks,
Himanshu
myDate is a String object. The replaceItemValue method does not know that you have put a date into that String, therefore it treats it as ordinary text. If uploadDBDate is a DateTime field, that causes a type mismatch during the computeWithForm operation.
The Lotus classes for Java include a DateTime class. The Session class has a createDateTime method that you pass a "mm/dd/yyyy" string and return a DateTime object. Then you can pass that DateTime object into replaceItemValue instead of passing in myDate.
I would recommend you to do those things:
1) disable computewithform and simply save document and then verify field UploadDBDate, does it have correct value? does it have corrrect type?
2) if everything is fine with UploadDBDate then there is a problem on a form, so try to investigate what calculation you do on the form, because problem is there.
In my JSP page I have a field which is date and when I getting as request.getParameter("dateVal"); gives me
15-Dec-2012 12:21.
I would like to pass this value to my database procedure and insert/update into table.
How can I pass the value as setDate using prepareCall to database?
Thanks
First step would be using SimpleDateFormat to parse it to a fullworthy java.util.Date instance in the controller:
Date date = new SimpleDateFormat("dd-MMM-yyyy HH:mm.", Locale.ENGLISH).parse(dateVal);
Then you can just create a java.sql.Date around its time in the database layer:
statement.setDate(1, new java.sql.Date(date.getTime()));
Unrelated to the concrete problem, please note that java.sql.Date doesn't remember the time part. If you have actually a DATETIME or TIMESTAMP field in the DB and not a DATE field, then rather use setTimestamp() with a java.sql.Timestamp instead. This way the time part will also be stored.
#BalusC 's answer is perfect. But as an alternative solution you can use the function provided by database to convert String to Date while querying. For example(in case you use Oracle),
to_date(date_in_String, format)
Try this :
new SimpleDateFormat("dd-MM-yyyy HH:mm").parse(mydate);
I managed in JAVA to store a calendar into a mysql DATETIME field
To fetch this value
entry.date = Calendar.getInstance(TimeZone.getTimeZone("UT"));
entry.date.setTime(rs.getDate(DBBLogEntries.entDate));
Where the entry.date is a java.util.Calendar
In the database the value is this: '2012-07-07 07:18:46'
I store all date values in a unique timezone in the db. ready to make all the extra work required to add or substract hours depending on the country from wich the request is comming.
The problem is that it brings the date but doesn't seem to brinng me the time.
Any sugestion please?
Thanks in advance.
Probably because Java has a different date format than mysql format(YYYY-MM-DD HH:MM:SS)
Visit the link :
http://www.coderanch.com/t/304851/JDBC/java/Java-date-MySQL-date-conversion
You may use SimpleDateFormat as follows.
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateTime = sdf.format(dt);
You should read a timestamp from the ResultSet object.
java.sql.Timestamp ts = rs.getTimestamp( DBBLogEntries.entDate );
Which returns a Timestamp instance that includes date and time.
don't use Java.util.Date ,use the Java.sql.Date.
Are you using the MySql DATE type? This does not preserve the time component.
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
Alternatively how are you retrieving the date from the db?