Java date checking help Then convert to VB.NET 2010
I have the following Java code that i am trying to dissect to better understand what all it is doing:
try {
String userName = System.getenv("USERNAME");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
OracleDataSource ds = new OracleDataSource();
ds.setDriverType("thin");
ds.setServerName("xxxxxxx");
ds.setPortNumber(151);
ds.setDatabaseName("uServer");
ds.setUser("userhere");
ds.setPassword("xxxxxxxxxxxx");
ResultSet rset = null;
Connection conn = ds.getConnection();
Statement stmt = conn.createStatement(rset.TYPE_SCROLL_SENSITIVE, rset.CONCUR_UPDATABLE);
rset = stmt.executeQuery("select status, due_date, subject, message, begin_date "
开发者_开发百科+ "from notifications "
+ "where status = 'OPEN' and recipient_role <> 'SYSADMIN' " + "and recipient_role = 'userhere' "
+ "order by due_date");
while (rset.next()) {
try {
String tempString = rset.getString("begin_date");
String message = rset.getString("message").replace("\r", "");
message = message.replace("\n", "");
String dateString = rset.getString("begin_date");
int len = dateString.length();
dateString = dateString.substring(0, len - 2);
java.util.Date tempDate = new java.util.Date();
if (dateFormat.parse(dateString).after(tempDate)) {
trayIcon.displayMessage(rset.getString("subject"), message, TrayIcon.MessageType.INFO);
}
Thread.sleep(1750);
} catch (Exception pe) {
pe.printStackTrace();
}
}
//Fill the table on the Form
JTable tbl = OracleWorkflowNotifierView.jTable;
rset.beforeFirst();
int rsetCount = 0;
while (rset.next()) {
rsetCount++;
}
if (rsetCount == 0) {
ImageIcon image = new ImageIcon(Toolkit.getDefaultToolkit().createImage(url_green));
trayIcon.setImage(image.getImage());
} else {
ImageIcon image = new ImageIcon(Toolkit.getDefaultToolkit().createImage(url_yellow));
trayIcon.setImage(image.getImage());
}
rset.beforeFirst();
while (rset.next()) {
java.util.Date today = new java.util.Date();
String endDate = rset.getString("due_date");
Calendar cal = Calendar.getInstance();
cal.setTime(today);
cal.add(Calendar.DATE, -3);
if (dateFormat.parse(endDate).after(cal.getTime())) {
ImageIcon image = new ImageIcon(Toolkit.getDefaultToolkit().createImage(url_red));
trayIcon.setImage(image.getImage());
}
}
I'm not sure what its compairing here?
if (dateFormat.parse(dateString).after(tempDate)) {
trayIcon.displayMessage(rset.getString("subject"), message, TrayIcon.MessageType.INFO);
}
Is it looking at the begin date minus the current date?
And then this one:
java.util.Date today = new java.util.Date();
String endDate = rset.getString("due_date");
Calendar cal = Calendar.getInstance();
cal.setTime(today);
cal.add(Calendar.DATE, -3);
if (dateFormat.parse(endDate).after(cal.getTime())) {
ImageIcon image = new ImageIcon(Toolkit.getDefaultToolkit().createImage(url_red));
trayIcon.setImage(image.getImage());
}
Is it looking at duedate minus 3 days from today?
Any help would be great! Thanks :o)
David
VB.NET CODE
So, taking the feedback below, would this be a suitable substitution for it in VB.net (the second part of my question)?
If Format(dr(1), "MM/dd/yyyy") >= DateAdd("d", 3, Format(Now, "MM/dd/yyyy")) Then
And would the first part of my question be:
If Format(dr(1), "MM/dd/yyyy") >= DateAdd("d", 1, Format(Now, "MM/dd/yyyy")) Then
Where dr(1) = dueDate in both cases above.
dateFormat.parse
takes a string and creates a Date object from it (if it is in the given format).
tempDate
is a Date object that is set to the current date and time. So
dateFormat.parse(dateString).after(tempDate)
is checking if the date that is returned from the database (begin_date) is after the current time.
In the second instance cal
is an instance of Calendar that was set to the current time, then set back by 3 days. So it is comparing the due_date
from the database with the current time 3 days ago.
(dateFormat.parse(dateString).after(tempDate))
evaluates to true if the date described by dateString
is after tempDate
.
dateFormat.parse(endDate).after(cal.getTime())
evaluates to true if the date described by endDate
is after today minus 3 days.
The first example is checking whether the begin_date (ignoring the last 2 characters, which I'm assuming are seconds) is in the future.
The second example is checking whether the due date is more than three days in the future ((due date - 3 days) is after now).
Yes, I believe both your conjectures are correct.
And you have my sympathies for having to deal with that kind of monolithic code.
精彩评论