我需要将一个Java Date
对象存储在Redis中。我正在使用Jedis作为我的Redis客户端。如何在Redis中存储Date对象并使用Java检索它?我看到的所有内容都使用String和Integer值。然而,我对这个世界的理解还不够,无法使用Date
对象进行操作。
//...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()));