Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if DateTime is in the future with old version of JodaTime

I have a unique situation. I'm working on a legacy project that is on an old WebLogic server that (A) does not allow anything past Java 6, and (B) pollutes the classloader with an old version of JodaTime (version 1.2, to be precise).

The client I'm working for has a Standard Development Platform that includes Java 8, and JodaTime for projects that are stuck in earlier versions of Java. Therefore, I'm stuck using this old version of JodaTime (310-Backport would be a great solution, but I'm not allowed to use it).

I need to create a utility method that checks if a DateTime is after today (regardless of time). JodaTime 1.2 does not have LocalDate, or convenient static factory methods like now(), so I have come up with this:

public static boolean isAfterToday(DateTime dateTime) {
  YearMonthDay date = new YearMonthDay(dateTime);
  YearMonthDay today = new YearMonthDay();
  return date.isAfter(today);
}

It feels a bit icky because in more recent versions of JodaTime, everything in the YearMonthDay class has been deprecated and replaced with LocalDate, which sadly I can't use. Is there a better way I can do this? Also, I'm trying to strip the time off the DateTime by converting it to a YearMonthDay... are there any "gotchas" I'm missing or should be aware of?

A couple of additional notes: I'm assuming that I've already null-checked the dateTime argument, and the time zone shouldn't be an issue because all time zones in the application are GMT. Also, I am allowed to use the Apache Commons and Guava libraries.

like image 218
James Dunn Avatar asked Nov 09 '22 02:11

James Dunn


1 Answers

Since you are sure that all time zones are GMT, the most trivial implementation would be:

public static boolean isAfterToday(DateTime dateTime) {
    String dateTimeStr = dateTime.toString("yyyy-MM-dd");
    String nowStr = new DateTime().toString("yyyy-MM-dd");
    return dateTimeStr.compareTo(nowStr) > 0;
}

You can slightly optimize it like this:

private static final DateTimeFormat yearMonthDay = DateTimeFormat.forPattern("yyyy-MM-dd");

public static boolean isAfterToday(DateTime dateTime) {
    String dateTimeStr = yearMonthDay.print(dateTime);
    String nowStr = yearMonthDay.print(new DateTime());
    return dateTimeStr.compareTo(nowStr) > 0;
}

That solution decouples you a bit from JodaTime in a way that it does not introduce another date/time type.

On the other hand, the deprecation note on YearMonthDay says

Use LocalDate which has a much better internal implementation

so it might be that it's not exactly wrong to use it and I would not assume that you run into any errors by using it.

I suggest writing a nice (parameterized?) set of unit tests for that method and validate the behaviour of YearMonthDay. If it seems to work, then go with it, if it does not, then try the toString() workaround.

like image 91
Adam Michalik Avatar answered Nov 14 '22 22:11

Adam Michalik