+
+ // 2 hours ago -> 18/10/2018 21:00
+ private Date getDate(String dateAgo) {
+ int h = 0;
+ if (dateAgo.endsWith("hour ago")) {
+ h = 1;
+ } else if (dateAgo.endsWith("hours ago")) {
+ dateAgo = dateAgo.replace("hours ago", "").trim();
+ h = Integer.parseInt(dateAgo);
+ } else if (dateAgo.endsWith("day ago")) {
+ h = 24;
+ } else if (dateAgo.endsWith("days ago")) {
+ dateAgo = dateAgo.replace("days ago", "").trim();
+ h = Integer.parseInt(dateAgo) * 24;
+ }
+
+ long now = new Date().getTime(); // in ms since 1970
+ now = now / (1000l * 60l * 60l); // in hours since 1970
+ long then = now - h; // in hours since 1970
+ then = then * (1000l * 60l * 60l); // in ms since 1970
+
+ return new Date(then);
+ }