コンパレーターを日付でソートする際に問題が発生しています。日付形式は「dd/MM/yyyy」にする必要があるため、SQL データベースから情報を呼び出し、次のようにして文字列を日付に変換します。
public void setDeadlineDate(String deadDate) throws ParseException {
this.d_date = deadDate;
//convert strings to dates
formatter = new SimpleDateFormat("dd/MM/yyyy");
convertedDeadlineDate = (Date)formatter.parse(deadDate);
}
次に、以下の get メソッドを作成して、コンパレータを呼び出します。例を挙げる必要がありますが、奇妙な日付が場違いであり、比較が正しくないという点で、常に矛盾があります。
例 1:
@Override
public int compare(JobRequest j1, JobRequest j2) {
if (j1.getConvertedDeadlineDate().before(j2.getConvertedDeadlineDate())) {
return -1;
} else if (j1.getConvertedDeadlineDate().after(j2.getConvertedDeadlineDate())) {
return 1;
}
else {
return 0;
}
}
例 2:
public int compare(JobRequest j1, JobRequest j2){
return j1.getConvertedDeadlineDate().compareTo(j2.getConvertedDeadlineDate());
}
これらの例の両方で問題が発生し、priorityqueue の締切日が希望どおりに正しい順序で表示されません。私のデータベースでは、日付関数を使用して英語形式にすることができないため、2012 年 12 月 1 日、「01/12/2012」という形式で varchar として保存されます。
文字列を変換してから比較するためのより良い方法ですか、それとも何か不足していますか?
ありがとう
編集:注文された日付の出力取得:
- 2011/05/04
- 2012/12/16
- 2012/06/18
- 2013/12/17
- 2013/12/17
- 2013/12/16
- 2013/12/17
- 2012/08/14
- 2013/12/19
PriortyQueue を宣言する場所:
private Comparator<JobRequest> comparator = new JobQueueComparator(); //calls my comparator
private PriorityQueue< JobRequest> scheduledJobs = new PriorityQueue<JobRequest>(100, comparator);
public void addJob(JobRequest job) {
// now add job to priority queue
scheduledJobs.add(job); // add jobs from the resultset into queue
}
scheduleJobs.add(job) は、結果セットからキューに入力するだけで、データベース内のすべてのフィールドが読み取られるまでキューに追加し続けます。以下を参照してください
public void populateQueueFromDB() {
// create priority queue
try {
String url = "jdbc:mysql://localhost:3306/project";
Connection conn = DriverManager.getConnection(url, "root", "nbuser");
PreparedStatement stmt = conn.prepareStatement("SELECT user_id,s_date,e_date,d_date,department,projectname,projectapplication,priority,cores,disk_space,analysis FROM booking");
ResultSet rs;
rs = stmt.executeQuery();
//List<JobRequest> jobList = new ArrayList<JobRequest>();
while (rs.next()) {
JobRequest job = new JobRequest();
User user = new User();
user.setUserID(rs.getString("user_id"));
job.setUserID(user.getUserID()); // changes the /user id to the job.setuser id so can call for my queue print.
job.setStartDate(rs.getString("s_date"));
job.setEndDate(rs.getString("e_date"));
job.setDeadlineDate(rs.getString("d_date"));
job.setDepartment(rs.getString("department"));
job.setProjectName(rs.getString("projectname"));
job.setProjectApplication(rs.getString("projectapplication"));
job.setPriority(rs.getInt("priority"));
job.setCores(rs.getInt("cores"));
job.setDiskSpace(rs.getInt("disk_space"));
job.setAnalysis(rs.getString("analysis"));
schedulerPriorityQueue.addJob( job );
}
schedulerPriorityQueue.printQueue();
conn.close();
} catch (Exception e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
}
印刷キュー:
public void printQueue() {
for (JobRequest jr : scheduledJobs) {
System.out.print(jr.getUserID() + "-->");
System.out.print(jr.getStartDate() + "--START-->");
System.out.print(jr.getEndDate() + "---END-->");
System.out.print(jr.getDeadDate() + "--DROP-->");
System.out.print(jr.getDepartment() + "-->");
System.out.print(jr.getProjectName() + "-->");
System.out.print(jr.getProjectApplication() + "-->");
System.out.print(jr.getPriority() + "--PRIORITY-->");
System.out.print(jr.getCores() + "-->");
System.out.print(jr.getDiskSpace() + "-->");
System.out.println(jr.getAnaylsis());
}
}