0

我想随机生成时间和数字。这里我用hashmap生成了一些记录。现在我可以生成数字,但我无法将它们分开。我必须分开这些值,以便我可以在数据库中设置它们。

这是我的代码...

public class DateTimePopulation {

private Random rand = new Random(); 
private Date theDay;
private String callDuration = null;
private String endDay = null;
SimpleDateFormat mytimeFormat = new SimpleDateFormat("HH:mm:ss");

public static void main(String[] args) throws ParseException {
    DateTimePopulation d = new DateTimePopulation();
    for (int i = 1; i <= 3; i++) {
        Map rec = d.getRecord();
    for (int j = 0; j < rec.size(); j++) {
            Collection c = rec.values();
            Iterator itr = c.iterator();
            int count=0;
            while (itr.hasNext()) {                 
                Object element=itr.next();
                System.out.println(element); 
            }
            System.out.println();
        }
    }
}

private Map getRecord() {
    Map<String, Object> rec = new LinkedHashMap<String, Object>();
    Date startDate;
    try {
        startDate = getRandStartDate();
        rec.put("StartTime", startDate);

        int start = 7200000, end = 0;
        int duration = getRandDuration(start, end);
        rec.put("Duration", duration);

        Date endDate = getRandEndDate(startDate, duration);
        rec.put("EndTime", endDate);

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return rec;
}



private Date getRandEndDate(Date startDate, int duration) {
    Random r = new Random();
    int ranSec = r.nextInt(duration - 0) + 0;
    return new Date(startDate.getTime() + ranSec * 1000);
}

private int getRandDuration(int High, int Low) {
    Random r = new Random();
    return r.nextInt(High - Low) + Low;
}

private Date getRandStartDate() throws ParseException {
    Date theDay = new SimpleDateFormat("yyyyMMdd hh:mm:ss")
            .parse("20130101 00:00:00");
    int High = 60 * 60 * 24 * 30 * 6;
    Random r = new Random();
    int Low = 10;
    int R = r.nextInt(High - Low) + Low;

    return new Date(theDay.getTime() + R * 1000);
}

}

这是输出。我正在展示 2 套。我必须分开时间,持续时间等。

Tue Jan 08 11:01:57 IST 2013
6074479
Fri Jan 18 12:56:24 IST 2013   
4

2 回答 2

1

Firstly, your design is strange to start with - why are you calling getRecord() on an instance, when it doesn't do anything with the fields of the object you're calling it on?

Additionally, when you're iterating over a map, you're actually iterating over the same map 3 times:

for (int j = 0; j < rec.size(); j++) {
    Collection c = rec.values();
    Iterator itr = c.iterator();
    int count=0;
    while (itr.hasNext()) {                 
        Object element=itr.next();
        System.out.println(element); 
    }
    System.out.println();
}

Your outer loop is pointless here - you're never using j, after all. I would probably iterate over the entries rather than the values, if you really want to - then you can print out the key which goes with each value.

Next, I would encourage you not to use a map for this at all - create a separate type with fields for start time, duration and end time. That will fix things very simply.

Next, if you still want to use a map, stop using the raw types - it'll make your life simpler.

Finally, if you really still want to use a map, you already know the keys, so there's no point in iterating over it:

System.out.println("Start: " + map.get("StartTime");
System.out.println("Duration: " + map.get("Duration");
System.out.println("End: " + map.get("EndTime");

Basically, I strongly suggest that you take a step back and revisit your whole design. You may well find it's better to start from scratch than to change your existing code.

于 2013-06-28T08:20:33.860 回答
0

Try to generify this code:

 private Map getRecord() {
        Map<String, Object> rec = new LinkedHashMap<String, Object>();
        Date startDate;
        try {
            startDate = getRandStartDate();
            rec.put("StartTime", startDate);

            int start = 7200000, end = 0;
            int duration = getRandDuration(start, end);
            rec.put("Duration", duration);

            Date endDate = getRandEndDate(startDate, duration);
            rec.put("EndTime", endDate);

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return rec;
    }

Something like that:

private DateInterval getRecord() {
    DateInterval interval = null;
    try {
        Date startDate = getRandStartDate();
        int start = 7200000, end = 0;
        int duration = getRandDuration(start, end);
        Date endDate = getRandEndDate(startDate, duration);

        interval = new DateInterval(startDate, endDate, duration);


    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return interval;
}

See also:

public class DateInterval {
    private Date startDate;
    private Date endDate;
    private int duration;

    public DateInterval(Date startDate, Date endDate, int duration) {
        this.startDate = startDate;
        this.endDate = endDate;
        this.duration = duration;
    }

    public Date getStartDate() {
        return startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public int getDuration() {
        return duration;
    }
}

I mean, you don't need Map for this purpose, use the your own entity. If you need to hold all your entities at Collection, use Map with DB UUIDs as a keys or List.

于 2013-06-28T08:19:44.283 回答