I've written some code for a client-server application which allows the server to set up 2 deadlines for 2 different items. These items have a deadline at which the server should display when the time reaches it.
Here is what I have so far:
String[] deadlines = new String[2];
Calendar deadline = Calendar.getInstance();
for(int i = 0; i < 2; i++)
{
System.out.print("Enter finishing time for item " + (i+1) + " in 24-hr format "); // Item 1
System.out.print("(e.g. 17:52) : ");
String timeString = input.nextLine(); // get input
String hourString = timeString.substring(0,2);
int hour = Integer.parseInt(hourString);
String minString = timeString.substring(3,5);
int minute = Integer.parseInt(minString);
deadline.set(year,month,date,hour,minute,0);
deadlines[i] = getDateTime(deadline);
System.out.print("\nDeadline set for item " + (i+1) + "\n");
System.out.println(getDateTime(deadline)+ "\n\n");
}
System.out.println("\nServer running...\n");
Calendar now = Calendar.getInstance();
System.out.print(deadlines[0]); // HERE
System.out.print(deadlines[1]); // AND HERE
// getDateTime(now) outputs the same as deadlines[0] + deadlines[1].
while(now.before(deadlines[0]) || now.before(deadlines[1])) // THIS LINE
{
//System.out.println(getDateTime(now));
try
{
Thread.sleep(2000);
}
catch (InterruptedException intEx)
{
}
now = Calendar.getInstance();
if (now.after(deadlines[0]))
System.out.println("\n\nDeadline reached" + deadlines[0] + "\n");
if (now.after(deadlines[1]))
System.out.println("\n\nDeadline reached" + deadlines[1] + "\n");
}
public static String getDateTime(Calendar dateTime)
{
//Extract hours and minutes, each with 2 digits
//(i.e., with leading zeroes if needed)...
String hour2Digits = String.format("%02d", dateTime.get(Calendar.HOUR_OF_DAY));
String min2Digits = String.format("%02d", dateTime.get(Calendar.MINUTE));
return(dateTime.get(Calendar.DATE)
+ "/" + (dateTime.get(Calendar.MONTH)+1)
+ "/" + dateTime.get(Calendar.YEAR)
+ " "+ hour2Digits + ":" + min2Digits);
}
I need to check whether now
is before the values of deadlines[0]
and deadlines[1]
. How can I do this? There must be a better way than converting it into a string etc?