我有一个包含一堆对象的数组列表,我为每个对象设置了一个日期字段,以便可以识别该对象的创建日期。 我希望在创建对象30天后显示该对象。
我试过了:
Object o2 = new Object("Mark", 23, "12/24/2015"); System.out.println(o2); //supposing that this is my object SimpleDateFormat formatDate = new SimpleDateFormat("MM/dd/yyyy"); // and this is a Simple format date. try { Date objDate = formatDate.parse(o2.getDate()); System.out.println(objDate); //And here i take form a string date into a date. } catch (ParseException ex) { System.out.println(ex.getMessage()); }但我不知道如何在创建对象30天后显示它。
I have a array list with a bunch of objects, and I set a date field for each object so it can be recognize on what date that object was created. I want to display that object 30 days after it was created.
I tried:
Object o2 = new Object("Mark", 23, "12/24/2015"); System.out.println(o2); //supposing that this is my object SimpleDateFormat formatDate = new SimpleDateFormat("MM/dd/yyyy"); // and this is a Simple format date. try { Date objDate = formatDate.parse(o2.getDate()); System.out.println(objDate); //And here i take form a string date into a date. } catch (ParseException ex) { System.out.println(ex.getMessage()); }But I don't know how display the object 30 days after it was created.
最满意答案
您不应该使用arraylist来实现此需求。 但是PriorityQueue会这样做。 您可以使用PriorityQueue存储您的对象,您的对象需要实现一个Comparator,它通过createdTime进行比较。
public class MyObject { private String name; private int id; private long time; } public static Comparator<MyObject> COMPARE = new Comparator<MyObject>() { @Override public int compare(MyObject o1, MyObject o2) { return (int) (o1.time - o2.time); } }; public PriorityQueue<MyObject> queue = new PriorityQueue<>(COMPARE);然后创建一个任务以从队列中轮询对象,如果创建了30天则显示该对象,否则再次添加到队列。
private class MyTask implements Runnable { @Override public void run() { while (true) { MyObject object = queue.poll(); if (object.time + DateUtils.MILLIS_PER_DAY * 30 > System.currentTimeMillis()) { display(object); } else { queue.add(object); } } } }您还可以使用redis的zadd和zrangeByScoreWithScores来获取按时间计算的socre数据。 所以,不需要轮询和添加多次。
You should not use arraylist to implement this demand. However PriorityQueue does. You can use PriorityQueue to store your object, your object need implement a Comparator which compare by their createdTime.
public class MyObject { private String name; private int id; private long time; } public static Comparator<MyObject> COMPARE = new Comparator<MyObject>() { @Override public int compare(MyObject o1, MyObject o2) { return (int) (o1.time - o2.time); } }; public PriorityQueue<MyObject> queue = new PriorityQueue<>(COMPARE);then create a task to poll object from queue, display the object if created 30 days, or else add to queue again.
private class MyTask implements Runnable { @Override public void run() { while (true) { MyObject object = queue.poll(); if (object.time + DateUtils.MILLIS_PER_DAY * 30 > System.currentTimeMillis()) { display(object); } else { queue.add(object); } } } }You also can use redis's zadd and zrangeByScoreWithScores to get data with socre computed by time. So that, do not need to poll and add multi times.
更多推荐
发布评论