我想随机生成时间和数字。这里我用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