0

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?

nopassport1
  • 1,821
  • 1
  • 25
  • 53
  • 7
    Having two date objects would be easier to compare than Strings. Use SimpleDateFormat to convert String to a Date object. – kosa Mar 08 '16 at 19:12
  • 3
    @Nambari Could you please show me how to do this? – nopassport1 Mar 08 '16 at 19:24
  • There are lot of examples on StackOverflow, simple search could help you, here is one: http://stackoverflow.com/questions/9872419/how-to-convert-a-string-to-a-date-using-simpledateformat – kosa Mar 08 '16 at 19:25
  • So, would I have to make `deadline` a `SimpleDateFormat` Date object? or do you mean make both that and the `now` Calendar instance? – nopassport1 Mar 08 '16 at 19:35
  • Make both Calendar instance and try to compare. – kosa Mar 08 '16 at 19:36
  • Is there any other ways of doing this or is this the most efficient way of doing this? I'm only really comparing the times. The dates are exactly the same – nopassport1 Mar 08 '16 at 19:38

2 Answers2

1

You may use LocalTime#parse, it takes the exact format you're wanting the user to enter.

String input = "17:52";
LocalTime lt = LocalTime.parse(input); // 17:52

Then pass it to a LocalDateTime#of with a LocalDate

LocalDateTime ldt = LocalDateTime.of(LocalDate.of(year, month, day), lt);

If you need to compare them, use compare(), isAfter() or isBefore()

while (LocalDateTime.now().isAfter(ldt)){
    // doStuff
}

EDIT

If you want to access the object outside of the loop, keep going with the same array logic if you want.

LocalDateTime[] dateTimes = new LocalDateTime[2];

for (//){
    datesTimes[i] // for example
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
0
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateTime = f.parseDateTime("2012-01-10 23:13:26");

DateTime now = new Date();

if(now.compareTo(dateTime)<=0){
   // dateTime is in the past
} 
Slava Shpitalny
  • 3,965
  • 2
  • 15
  • 22