We don’t know. The strings don’t give the same information, so the information from them is not readily comparable. If you know which time zone is assumed in the second string, we convert each to a point in time and compare them.
The former string is the easier one since in addition to date and time of day it also informs us of an offset from UTC (a time zone offset). So with this string we can identify a point in time. Furthermore the string is in ISO 8601 format. The classes of java.time, the modern Java date and time API, parse ISO 8601 format (or the most common variants of it) as their default that is, without any explicit formatter.
String responseFromWs = "2020-01-07T17:15:00-08:00";
OffsetDateTime odtFromWs = OffsetDateTime.parse(responseFromWs);
System.out.println(odtFromWs);
Output this far is:
2020-01-07T17:15-08:00
It looks very much like the input (only the 00
seconds have been omitted). This is because OffsetDateTime
also prints ISO 8601 format back (and the seconds are optional according to that format). The important thing is we’ve got a date-time object that we can use for further processing, like comparing to other date-time objects.
The trouble with the second string, the one from the database, is we don’t know the implied offset. Common recommendations include storing date and time in UTC in your database and also not to fetch it as a string from the database but as a proper date-time object, for example an OFfsetDateTime
like the one we used before.
If you know that the string is in UTC and we rely on the string for now:
DateTimeFormatter dbFormatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE)
.appendLiteral(' ')
.append(DateTimeFormatter.ISO_LOCAL_TIME)
.toFormatter();
String responseFromDatabase = "2020-01-08 01:15:00.0";
OffsetDateTime odtFromDb = LocalDateTime
.parse(responseFromDatabase, dbFormatter)
.atOffset(ZoneOffset.UTC);
System.out.println(odtFromDb);
2020-01-08T01:15Z
Now we have got two OffsetDateTime
objects. They have got methods for comparison:
System.out.println("Before: " + odtFromWs.isBefore(odtFromDb));
System.out.println("After: " + odtFromWs.isAfter(odtFromDb));
System.out.println("Same time: " + odtFromWs.isEqual(odtFromDb));
System.out.println("Equal: " + odtFromWs.equals(odtFromDb));
Before: false
After: false
Same time: true
Equal: false
You may be surprised that isEqual()
returns true and equals()
false. This is because isEqual()
tests whether the two objects denote the same point in time, which they do, while equals()
requires the same point in time and the same offset before it yields true. Since the two objects have different offsets, it returns false in this case.
Your JDBC driver or JPA implementation will probably be happy to fetch a date-time object from the database rather than the string, which, as I said, is recommended. The details depend on the datatype used in the database, but you can read more in my answer to a different questions, see the link at the bottom.
Links