All jobs

Migrate a Java Project From Joda-Time to java.time

Yes. An agent ports Joda-Time code to java.time and runs both on the same inputs first, so differences in parsing and time zone gaps are decided, not discovered.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The two libraries share class names and an author, which makes this migration look like a change of imports. The agent assumes the opposite until it has evidence, and gathers it before editing: it lists every Joda type and call, then writes a small harness that feeds the same strings to the Joda code and to the proposed java.time code and prints both results. In a library like aaronshan/hive-third-functions, a set of Hive user-defined functions, Joda-Time 2.7 is used in six classes: UDFDayOfYear, UDFDayOfWeek, UDFTypeOfDay, the two zodiac functions, and UDFSequence. Five of them parse a date with DateTimeFormat.forPattern("yyyy-MM-dd"), and the sixth parses timestamps with "yyyy-MM-dd HH:mm:ss".

The harness shows that the obvious translation, DateTimeFormatter.ofPattern("yyyy-MM-dd"), is a different parser. Joda accepts 2016-2-9 and reads it as 9 February; java.time throws, because MM and dd there mean exactly two digits. Joda rejects 2016-02-30; java.time in its default resolver mode accepts it and quietly returns 29 February. In most programs those would surface as exceptions. Here they would not, because the date functions wrap the parse in try and answer catch (Exception e) { return null; }. After a plain port, day_of_year('2016-2-9') would change from 40 to NULL and day_of_year('2016-02-30') from NULL to 60, in query results, with nothing logged. The agent picks a formatter that reproduces the old answers, the pattern u-M-d with ResolverStyle.STRICT, and shows the two tables side by side.

The rest of the port is type by type. LocalDate.getDayOfWeek() returned an int in Joda and returns a DayOfWeek enum now, so date.getDayOfWeek() < 6 in UDFTypeOfDay no longer compiles; the agent uses getValue(), which keeps Monday as 1 and Sunday as 7. LocalDate.fromCalendarFields(calendar) has no counterpart, and the timestamp overloads go through Timestamp.toLocalDateTime() instead. getMonthOfYear() becomes getMonthValue(). UDFSequence needs the most care, since it turns local date-time strings into epoch milliseconds with DateTime.parse(...).getMillis(), which uses the default time zone. For a wall-clock time inside a daylight saving gap, Joda throws an IllegalInstantException; LocalDateTime.atZone moves the time forward by the length of the gap and continues. The agent keeps the port faithful by checking for the gap with the zone's rules, and names the line to delete if you prefer the lenient behaviour.

Two build facts decide whether any of this can ship. java.time needs Java 8, and this pom.xml sets project.build.targetJdk to 1.6 for both source and target of the compiler plugin, while .travis.yml still lists openjdk7 beside oraclejdk8. The agent raises the level to 1.8, removes the Java 7 entry, and states plainly that the jar will no longer load on older runtimes. Then it removes the joda-time dependency and its version property, rebuilds with mvn clean package, and checks the dependency tree and the shaded jar the build produces, to see whether org.joda classes still arrive through Hive or another dependency. It adds tests for the functions it touched, because of the 17 test classes in the repository only UDFDayOfYearTest covers one, with a single input.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "codex",
  "input": "Migrate this Java project from Joda-Time to java.time. Read the build file for the Java source and target level, the Joda-Time version, and any shading or packaging step; read the CI configuration for the JDKs it builds on. java.time needs Java 8: if the level is lower, raise it, update CI to match, and say what stops working. List every Joda import and call by class. Before changing code, write a throwaway harness that runs the existing Joda code path and your proposed java.time replacement on the same inputs and prints both: for every date pattern in the code, a normal value, a value with single-digit month and day, an impossible date such as 30 February, a two-digit year, and a value with trailing text; for every conversion between local date-times and instants, a time inside a daylight saving gap and one inside an overlap in a zone that has them. Where the results differ, choose the java.time code that reproduces the Joda result (pattern letters, ResolverStyle, an explicit gap check) unless that is impossible, and report each case. Pay attention to code that catches exceptions and returns null or a default, because a parsing difference there changes results silently. Port the code, remove the dependency, and check the dependency tree and the packaged jar for remaining org.joda classes. Add unit tests using the harness inputs. Run the full build and tests. Open a pull request with the comparison tables and the build output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that changes the six classes, pom.xml and .travis.yml, and adds test classes beside the existing UDFDayOfYearTest. The description opens with the comparison tables, one per pattern: each input, the Joda result, the result of the naive java.time translation, and the result of the code that was committed. The third and fourth columns differ in exactly the rows that explain why the committed formatter is not the obvious one. A second table does the same for the gap and overlap times in UDFSequence.

A reviewer reads the tables to confirm that every row of the committed column equals the Joda column, or carries a note saying why it could not. They check the compiler level change against the runtimes their clusters use, since that is the one part of the pull request that can break a deployment and not just a result. The description ends with the dependency tree excerpt and the list of org.joda entries left in the packaged jar, if any, with the dependency that brought each one.

Run it on every repository that depends on joda-time

This is a once-per-repository job, and the list of repositories is something you already have from a dependency search. From a script, send the request once for each, with that repository as the resources URL and a request_id that is a UUID v5 of the repository name, so a second pass over the same list starts nothing twice. Each key runs up to its max_concurrent limit at once and the rest are created queued.

If you would rather it arrive as part of regular maintenance, add an automation on each Project with a schedule trigger and a weekly cron. The prompt is the only instruction, so begin it with a check for joda-time in the build file; a run that finds none reports that and ends.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

The dangerous differences between these libraries produce no compile error and no failed test in a codebase whose tests use well-formed dates. They show up when both libraries are run on awkward inputs, and that requires a build with Joda still present, a JDK, and a zone database. The agent sets that up in a sandbox, runs the harness, and keeps its output, so the tables in the pull request are printed by the two libraries themselves and can be reproduced by anyone who doubts a row.

It also separates the decisions from the typing. Most of the diff is renames a reviewer can skim. The few places where behaviour could change are listed with both options and the input that tells them apart, which turns a migration review from reading every line into answering three or four concrete questions.

Frequently asked questions

Can I keep my date patterns as they are?

The letters mostly mean the same, but the parsers treat width differently. In java.time, MM parses exactly two digits, while Joda's parser also accepts one. If your inputs are always zero-padded, yyyy-MM-dd behaves the same for valid dates. If they come from users or other systems, use single letters such as M and d, which accept one or two digits.

Why does java.time accept 30 February?

The default resolver style is SMART, which clamps a day that is too large for the month to the month's last day. Add .withResolverStyle(ResolverStyle.STRICT) to reject it. In strict mode the year letter in the pattern must be u, not y, because y is year-of-era and strict resolution then also demands an era.

What replaces DateTime?

It depends on what the code meant. A moment with a zone is ZonedDateTime, a moment with only an offset is OffsetDateTime, a point on the timeline is Instant, and a wall-clock value with no zone is LocalDateTime. Joda code often used DateTime for all four, so the agent picks per call site and lists its choices.

Will Joda-Time disappear from my jar?

Only if nothing else needs it. Frameworks and older libraries often depend on Joda-Time themselves, and it then stays on the classpath after your own code stops importing it. The agent reports the dependency path so you can see which upgrade would remove the last copy.

More jobs like this: Upgrade dependencies weekly

Keep reading