Kotlinlearncs.online LogoJava
Return to List

Test Writing: Compare Date Strings

Created By: Geoffrey Challen
/ Version: 2021.9.0

Complete the method below called compareDates that, given two Strings containing datetimes in ISO-8601 format, returns -1 if the first time is before the second, 1 if the second is before the first, and 0 if they are equal.

When you are done, here is how your method should work:

You will want to approach this in two steps. First, convert each String into some kind of datetime representation. We suggest that you explore the various built-in Java libraries for working with dates and times. Don't attempt to do this yourself!

Next, use the resulting object to compare the two datetimes. We might suggest that you explore the java.time.Instant class and its parse and other methods. We have provided some starter code so that you can identify where the import statements should go. Ignore the class Question stuff, since we haven't covered that yet.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestQuestion with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods
// Import statements here
class Question {
fun compareDates(
first: String,
second: String,
): Int {
return 0
}
}