Storing Java Date Object in REDIS
I need to store a Java object Date
in Redis. I am using Jedis as my Redis client. How can I store a Date object in Redis and retrieve it using Java? Everything I can see is using String and Integer values. However, I don't understand enough about this world to do it with an object Date
.
source to share
How can I convert a Date object to an Epoch timestamp and convert it back to a Date with any date format? This way you won't run into problems 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 it helps.
source to share
You need to find a way to serialize the data and analyze it later. Redis will only store strings and Jedis does not provide serializers. This conversation contains more information about your use case.
source to share