Skip to main content

Prefer the Java standard library instead of Joda-Time

org.openrewrite.java.joda.time.NoJodaTime

Before Java 8, Java lacked a robust date and time library, leading to the widespread use of Joda-Time to fill this gap. With the release of Java 8, the java.time package was introduced, incorporating most of Joda-Time's concepts. Features deemed too specialized or bulky for java.time were included in the ThreeTen-Extra library. This recipe migrates Joda-Time types to java.time and threeten-extra types.

Tags​

Recipe source​

GitHub: no-joda-time.yml, Issue Tracker, Code Genome Project

Info

This recipe is composed of more than one recipe. If you want to customize the set of recipes this is composed of, you can find and copy the GitHub source for the recipe from the link above.

This recipe is available under the Moderne Source Available License. Moderne customers can download precompiled artifacts from The Code Genome Project. For non-commercial use you can build the artifact from source locally.

Definition​

Used by​

This recipe is used as part of the following composite recipes:

Examples​

Example 1​

JodaAbstractInstantToJavaTimeTest#migrateAbstractInstant

Before​
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Instant;
import org.joda.time.format.DateTimeFormat;

class A {
public void foo() {
new DateTime().equals(DateTime.now());
new DateTime().getZone();
new DateTime().isAfter(1234567890L);
new Instant().isAfter(1234567890L);
new DateTime().isAfter(DateTime.now().minusDays(1));
new Instant().isAfter(Instant.now().minus(Duration.standardDays(1)));
new DateTime().isBefore(1234567890L);
new Instant().isBefore(1234567890L);
new DateTime().isBefore(DateTime.now().plusDays(1));
new Instant().isBefore(Instant.now().plus(Duration.standardDays(1)));
new DateTime().isBeforeNow();
new DateTime().isEqual(1234567890L);
new DateTime().isEqual(DateTime.now().plusDays(1));
new DateTime().toDate();
new DateTime().toInstant();
new DateTime().toString();
new DateTime().toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
}
}
After​
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

class A {
public void foo() {
ZonedDateTime.now().equals(ZonedDateTime.now());
ZonedDateTime.now().getZone();
ZonedDateTime.now().isAfter(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
Instant.now().isAfter(Instant.ofEpochMilli(1234567890L));
ZonedDateTime.now().isAfter(ZonedDateTime.now().minusDays(1));
Instant.now().isAfter(Instant.now().minus(Duration.ofDays(1)));
ZonedDateTime.now().isBefore(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
Instant.now().isBefore(Instant.ofEpochMilli(1234567890L));
ZonedDateTime.now().isBefore(ZonedDateTime.now().plusDays(1));
Instant.now().isBefore(Instant.now().plus(Duration.ofDays(1)));
ZonedDateTime.now().isBefore(ZonedDateTime.now());
ZonedDateTime.now().isEqual(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
ZonedDateTime.now().isEqual(ZonedDateTime.now().plusDays(1));
Date.from(ZonedDateTime.now().toInstant());
ZonedDateTime.now().toInstant();
ZonedDateTime.now().toString();
ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
}
}

Example 2​

JodaDateMidnightToJavaTimeTest#dateMidnightConstructor

Before​
import org.joda.time.DateMidnight;

class A {
public void foo() {
new DateMidnight();
}
}
After​
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;

class A {
public void foo() {
LocalDate.now().atStartOfDay(ZoneOffset.of(ZoneId.systemDefault().getId()));
}
}

Example 3​

JodaDateTimeToJavaTimeTest#migrateNewDateTime

Before​
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;

class A {
public void foo() {
new DateTime();
new DateTime(DateTimeZone.UTC);
new DateTime(1234567890L);
new DateTime(1234567890L, DateTimeZone.forID("America/New_York"));
new DateTime(2024, 9, 30, 12, 58);
new DateTime(2024, 9, 30, 12, 58, DateTimeZone.forOffsetHours(2));
new DateTime(2024, 9, 30, 13, 3, 15);
new DateTime(2024, 9, 30, 13, 3, 15, DateTimeZone.forOffsetHoursMinutes(5, 30));
new DateTime(2024, 9, 30, 13, 49, 15, 545);
new DateTime(2024, 9, 30, 13, 49, 15, 545, DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York")));
}
}
After​
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.TimeZone;

class A {
public void foo() {
ZonedDateTime.now();
ZonedDateTime.now(ZoneOffset.UTC);
ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.systemDefault());
ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.of("America/New_York"));
ZonedDateTime.of(2024, 9, 30, 12, 58, 0, 0, ZoneId.systemDefault());
ZonedDateTime.of(2024, 9, 30, 12, 58, 0, 0, ZoneOffset.ofHours(2));
ZonedDateTime.of(2024, 9, 30, 13, 3, 15, 0, ZoneId.systemDefault());
ZonedDateTime.of(2024, 9, 30, 13, 3, 15, 0, ZoneOffset.ofHoursMinutes(5, 30));
ZonedDateTime.of(2024, 9, 30, 13, 49, 15, 545 * 1_000_000, ZoneId.systemDefault());
ZonedDateTime.of(2024, 9, 30, 13, 49, 15, 545 * 1_000_000, TimeZone.getTimeZone("America/New_York").toZoneId());
}
}

Example 4​

JodaDateTimeZoneToJavaTimeTest#migrateDateTimeZone

Before​
import org.joda.time.DateTimeZone;
import java.util.TimeZone;

class A {
public void foo() {
DateTimeZone.UTC.toString();
DateTimeZone.forID("America/New_York");
DateTimeZone.forOffsetHours(2);
DateTimeZone.forOffsetHoursMinutes(5, 30);
DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York"));
}
}
After​
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.TimeZone;

class A {
public void foo() {
ZoneOffset.UTC.toString();
ZoneId.of("America/New_York");
ZoneOffset.ofHours(2);
ZoneOffset.ofHoursMinutes(5, 30);
TimeZone.getTimeZone("America/New_York").toZoneId();
}
}

Example 5​

JodaDurationToJavaTimeTest#migrateJodaDuration

Before​
import org.joda.time.Duration;

class A {
public void foo() {
Duration.standardDays(1L);
Duration.standardHours(1L);
Duration.standardMinutes(1L);
Duration.standardSeconds(1L);
Duration.millis(1000L);
new Duration(1000L);
new Duration(1000L, 2000L);
new Duration(1000L).getStandardDays();
new Duration(1000L).getStandardHours();
new Duration(1000L).getStandardMinutes();
new Duration(1000L).getStandardSeconds();
new Duration(1000L).toDuration();
new Duration(1000L).withMillis(2000L);
new Duration(1000L).withDurationAdded(550L, 2);
new Duration(1000L).withDurationAdded(new Duration(550L), 2);
new Duration(1000L).plus(550L);
new Duration(1000L).plus(new Duration(550L));
new Duration(1000L).minus(550L);
new Duration(1000L).minus(new Duration(550L));
new Duration(1000L).multipliedBy(2);
new Duration(1000L).dividedBy(2);
new Duration(1000L).negated();
new Duration(1000L).abs();
}
}
After​
import java.time.Duration;
import java.time.Instant;

class A {
public void foo() {
Duration.ofDays(1L);
Duration.ofHours(1L);
Duration.ofMinutes(1L);
Duration.ofSeconds(1L);
Duration.ofMillis(1000L);
Duration.ofMillis(1000L);
Duration.between(Instant.ofEpochMilli(1000L), Instant.ofEpochMilli(2000L));
Duration.ofMillis(1000L).toDays();
Duration.ofMillis(1000L).toHours();
Duration.ofMillis(1000L).toMinutes();
Duration.ofMillis(1000L).getSeconds();
Duration.ofMillis(1000L);
Duration.ofMillis(2000L);
Duration.ofMillis(1000L).plusMillis(550L * 2);
Duration.ofMillis(1000L).plus(Duration.ofMillis(550L).multipliedBy(2));
Duration.ofMillis(1000L).plusMillis(550L);
Duration.ofMillis(1000L).plus(Duration.ofMillis(550L));
Duration.ofMillis(1000L).minusMillis(550L);
Duration.ofMillis(1000L).minus(Duration.ofMillis(550L));
Duration.ofMillis(1000L).multipliedBy(2);
Duration.ofMillis(1000L).dividedBy(2);
Duration.ofMillis(1000L).negated();
Duration.ofMillis(1000L).abs();
}
}

Example 6​

JodaFormatterToJavaTimeTest#migrateDateTimeFormat

Before​
import org.joda.time.format.DateTimeFormat;

class A {
public void foo() {
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTimeFormat.shortDate();
DateTimeFormat.mediumDate();
DateTimeFormat.longDate();
DateTimeFormat.fullDate();
DateTimeFormat.shortTime();
DateTimeFormat.mediumTime();
DateTimeFormat.longTime();
DateTimeFormat.fullTime();
DateTimeFormat.shortDateTime();
DateTimeFormat.mediumDateTime();
DateTimeFormat.longDateTime();
DateTimeFormat.fullDateTime();
}
}
After​
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

class A {
public void foo() {
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.LONG);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL);
}
}

Example 7​

JodaInstantToJavaTimeTest#migrateInstant

Before​
import org.joda.time.Instant;
import org.joda.time.Duration;

class A {
public void foo() {
new Instant();
Instant.now().getMillis();
Instant.now().minus(Duration.standardDays(1L));
Instant.ofEpochMilli(1234567890L);
Instant.parse("2024-10-25T15:45:00");
Instant.now().plus(Duration.standardDays(1L));
}
}
After​
import java.time.Duration;
import java.time.Instant;

class A {
public void foo() {
Instant.now();
Instant.now().toEpochMilli();
Instant.now().minus(Duration.ofDays(1L));
Instant.ofEpochMilli(1234567890L);
Instant.parse("2024-10-25T15:45:00");
Instant.now().plus(Duration.ofDays(1L));
}
}

Example 8​

JodaIntervalToJavaTimeTest#migrateInterval

Before​
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;
import org.joda.time.DateTimeZone;

class A {
public void foo() {
new Interval(50, 100);
new Interval(50, 100, DateTimeZone.UTC);
new Interval(DateTime.now(), DateTime.now().plusDays(1));
new Interval(DateTime.now(), Duration.standardDays(1));
}
}
After​
import org.threeten.extra.Interval;

import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;

class A {
public void foo() {
Interval.of(Instant.ofEpochMilli(50), Instant.ofEpochMilli(100));
Interval.of(Instant.ofEpochMilli(50), Instant.ofEpochMilli(100));
Interval.of(ZonedDateTime.now().toInstant(), ZonedDateTime.now().plusDays(1).toInstant());
Interval.of(ZonedDateTime.now().toInstant(), Duration.ofDays(1));
}
}

Example 9​

JodaLocalDateToJavaTimeTest#migrateLocalDate

Before​
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

class A {
public void foo() {
new LocalDate();
new LocalDate(DateTimeZone.UTC);
new LocalDate(2024, 10, 25);
new LocalDate(1234567890L);
new LocalDate(1234567890L, DateTimeZone.UTC);
LocalDate.now().getDayOfWeek();
LocalDate.now().getMonthOfYear();
LocalDate.now().withMonthOfYear(6);
LocalDate.now().plusDays(1);
LocalDate.now().toDateTimeAtStartOfDay();
LocalDate.now().toDateTimeAtStartOfDay(DateTimeZone.UTC);
LocalDate.now().toLocalDateTime(new LocalTime(10, 30));
}
}
After​
import java.time.*;

class A {
public void foo() {
LocalDate.now();
LocalDate.now(ZoneOffset.UTC);
LocalDate.of(2024, 10, 25);
Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()).toLocalDate();
Instant.ofEpochMilli(1234567890L).atZone(ZoneOffset.UTC).toLocalDate();
LocalDate.now().getDayOfWeek().getValue();
LocalDate.now().getMonthValue();
LocalDate.now().withMonth(6);
LocalDate.now().plusDays(1);
LocalDate.now().atStartOfDay(ZoneId.systemDefault());
LocalDate.now().atStartOfDay(ZoneOffset.UTC);
LocalDate.now().atTime(LocalTime.of(10, 30));
}
}

Example 10​

JodaLocalTimeToJavaTimeTest#migrateLocalTime

Before​
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;

class A {
public void foo() {
new LocalTime();
new LocalTime(DateTimeZone.UTC);
new LocalTime(10, 30);
new LocalTime(10, 30, 45);
new LocalTime(10, 30, 45, 500);
LocalTime.now().plusMillis(100);
LocalTime.now().minusMillis(100);
LocalTime.now().withMillisOfSecond(500);
LocalTime.now().getMillisOfSecond();
LocalTime.now().getMillisOfDay();
LocalTime.now().getHourOfDay();
LocalTime.now().getMinuteOfHour();
LocalTime.now().getSecondOfMinute();
LocalTime.now().withHourOfDay(10);
LocalTime.now().withMinuteOfHour(30);
LocalTime.now().withSecondOfMinute(45);
LocalTime.now().toDateTimeToday();
LocalTime.now().toDateTimeToday(DateTimeZone.UTC);
}
}
After​
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;

class A {
public void foo() {
LocalTime.now();
LocalTime.now(ZoneOffset.UTC);
LocalTime.of(10, 30);
LocalTime.of(10, 30, 45);
LocalTime.of(10, 30, 45, 500 * 1_000_000);
LocalTime.now().plusNanos(100 * 1_000_000L);
LocalTime.now().minusNanos(100 * 1_000_000L);
LocalTime.now().withNano(500 * 1_000_000);
LocalTime.now().get(ChronoField.MILLI_OF_SECOND);
LocalTime.now().get(ChronoField.MILLI_OF_DAY);
LocalTime.now().getHour();
LocalTime.now().getMinute();
LocalTime.now().getSecond();
LocalTime.now().withHour(10);
LocalTime.now().withMinute(30);
LocalTime.now().withSecond(45);
LocalTime.now().atDate(LocalDate.now()).atZone(ZoneId.systemDefault());
LocalTime.now().atDate(LocalDate.now(ZoneOffset.UTC)).atZone(ZoneOffset.UTC);
}
}

Example 11​

JodaTimePeriodToJavaTimeTest#daysBetweenGetDays

Before​
import org.joda.time.DateTime;
import org.joda.time.Days;

class A {
void foo(DateTime start, DateTime end) {
int days = Days.daysBetween(start, end).getDays();
}
}
After​
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

class A {
void foo(ZonedDateTime start, ZonedDateTime end) {
int days = (int) ChronoUnit.DAYS.between(start, end);
}
}

Example 12​

JodaTimeRecipeTest#migrateSafeVariable

Before​
import org.joda.time.DateTime;

class A {
public void foo() {
DateTime dt = new DateTime();
dt.toDateTime().toString();
}
}
After​
import java.time.ZonedDateTime;

class A {
public void foo() {
ZonedDateTime dt = ZonedDateTime.now();
dt.toString();
}
}

Example 13​

NoJodaTimeTest#migrateJodaTime

Before​
import org.joda.time.DateTime;
import org.joda.time.Interval;

class A {
void foo() {
DateTime dt = new DateTime();
DateTime dt1 = new DateTime().plusDays(1);
Interval i = new Interval(dt, dt1);
i.toDuration();
}
}
After​
import org.threeten.extra.Interval;

import java.time.ZonedDateTime;

class A {
void foo() {
ZonedDateTime dt = ZonedDateTime.now();
ZonedDateTime dt1 = ZonedDateTime.now().plusDays(1);
Interval i = Interval.of(dt.toInstant(), dt1.toInstant());
i.toDuration();
}
}
Unchanged​
foo
Before​
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.foobar</groupId>
<artifactId>foobar-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</project>
After​
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.foobar</groupId>
<artifactId>foobar-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threeten-extra</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</project>

Example 14​

JodaAbstractInstantToJavaTimeTest#migrateAbstractInstant

Before​
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Instant;
import org.joda.time.format.DateTimeFormat;

class A {
public void foo() {
new DateTime().equals(DateTime.now());
new DateTime().getZone();
new DateTime().isAfter(1234567890L);
new Instant().isAfter(1234567890L);
new DateTime().isAfter(DateTime.now().minusDays(1));
new Instant().isAfter(Instant.now().minus(Duration.standardDays(1)));
new DateTime().isBefore(1234567890L);
new Instant().isBefore(1234567890L);
new DateTime().isBefore(DateTime.now().plusDays(1));
new Instant().isBefore(Instant.now().plus(Duration.standardDays(1)));
new DateTime().isBeforeNow();
new DateTime().isEqual(1234567890L);
new DateTime().isEqual(DateTime.now().plusDays(1));
new DateTime().toDate();
new DateTime().toInstant();
new DateTime().toString();
new DateTime().toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
}
}
After​
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

class A {
public void foo() {
ZonedDateTime.now().equals(ZonedDateTime.now());
ZonedDateTime.now().getZone();
ZonedDateTime.now().isAfter(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
Instant.now().isAfter(Instant.ofEpochMilli(1234567890L));
ZonedDateTime.now().isAfter(ZonedDateTime.now().minusDays(1));
Instant.now().isAfter(Instant.now().minus(Duration.ofDays(1)));
ZonedDateTime.now().isBefore(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
Instant.now().isBefore(Instant.ofEpochMilli(1234567890L));
ZonedDateTime.now().isBefore(ZonedDateTime.now().plusDays(1));
Instant.now().isBefore(Instant.now().plus(Duration.ofDays(1)));
ZonedDateTime.now().isBefore(ZonedDateTime.now());
ZonedDateTime.now().isEqual(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
ZonedDateTime.now().isEqual(ZonedDateTime.now().plusDays(1));
Date.from(ZonedDateTime.now().toInstant());
ZonedDateTime.now().toInstant();
ZonedDateTime.now().toString();
ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
}
}

Example 15​

JodaDateMidnightToJavaTimeTest#dateMidnightConstructor

Before​
import org.joda.time.DateMidnight;

class A {
public void foo() {
new DateMidnight();
}
}
After​
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZoneOffset;

class A {
public void foo() {
LocalDate.now().atStartOfDay(ZoneOffset.of(ZoneId.systemDefault().getId()));
}
}

Example 16​

JodaDateTimeToJavaTimeTest#migrateNewDateTime

Before​
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import java.util.TimeZone;

class A {
public void foo() {
new DateTime();
new DateTime(DateTimeZone.UTC);
new DateTime(1234567890L);
new DateTime(1234567890L, DateTimeZone.forID("America/New_York"));
new DateTime(2024, 9, 30, 12, 58);
new DateTime(2024, 9, 30, 12, 58, DateTimeZone.forOffsetHours(2));
new DateTime(2024, 9, 30, 13, 3, 15);
new DateTime(2024, 9, 30, 13, 3, 15, DateTimeZone.forOffsetHoursMinutes(5, 30));
new DateTime(2024, 9, 30, 13, 49, 15, 545);
new DateTime(2024, 9, 30, 13, 49, 15, 545, DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York")));
}
}
After​
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.TimeZone;

class A {
public void foo() {
ZonedDateTime.now();
ZonedDateTime.now(ZoneOffset.UTC);
ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.systemDefault());
ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.of("America/New_York"));
ZonedDateTime.of(2024, 9, 30, 12, 58, 0, 0, ZoneId.systemDefault());
ZonedDateTime.of(2024, 9, 30, 12, 58, 0, 0, ZoneOffset.ofHours(2));
ZonedDateTime.of(2024, 9, 30, 13, 3, 15, 0, ZoneId.systemDefault());
ZonedDateTime.of(2024, 9, 30, 13, 3, 15, 0, ZoneOffset.ofHoursMinutes(5, 30));
ZonedDateTime.of(2024, 9, 30, 13, 49, 15, 545 * 1_000_000, ZoneId.systemDefault());
ZonedDateTime.of(2024, 9, 30, 13, 49, 15, 545 * 1_000_000, TimeZone.getTimeZone("America/New_York").toZoneId());
}
}

Example 17​

JodaDateTimeZoneToJavaTimeTest#migrateDateTimeZone

Before​
import org.joda.time.DateTimeZone;
import java.util.TimeZone;

class A {
public void foo() {
DateTimeZone.UTC.toString();
DateTimeZone.forID("America/New_York");
DateTimeZone.forOffsetHours(2);
DateTimeZone.forOffsetHoursMinutes(5, 30);
DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York"));
}
}
After​
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.TimeZone;

class A {
public void foo() {
ZoneOffset.UTC.toString();
ZoneId.of("America/New_York");
ZoneOffset.ofHours(2);
ZoneOffset.ofHoursMinutes(5, 30);
TimeZone.getTimeZone("America/New_York").toZoneId();
}
}

Example 18​

JodaDurationToJavaTimeTest#migrateJodaDuration

Before​
import org.joda.time.Duration;

class A {
public void foo() {
Duration.standardDays(1L);
Duration.standardHours(1L);
Duration.standardMinutes(1L);
Duration.standardSeconds(1L);
Duration.millis(1000L);
new Duration(1000L);
new Duration(1000L, 2000L);
new Duration(1000L).getStandardDays();
new Duration(1000L).getStandardHours();
new Duration(1000L).getStandardMinutes();
new Duration(1000L).getStandardSeconds();
new Duration(1000L).toDuration();
new Duration(1000L).withMillis(2000L);
new Duration(1000L).withDurationAdded(550L, 2);
new Duration(1000L).withDurationAdded(new Duration(550L), 2);
new Duration(1000L).plus(550L);
new Duration(1000L).plus(new Duration(550L));
new Duration(1000L).minus(550L);
new Duration(1000L).minus(new Duration(550L));
new Duration(1000L).multipliedBy(2);
new Duration(1000L).dividedBy(2);
new Duration(1000L).negated();
new Duration(1000L).abs();
}
}
After​
import java.time.Duration;
import java.time.Instant;

class A {
public void foo() {
Duration.ofDays(1L);
Duration.ofHours(1L);
Duration.ofMinutes(1L);
Duration.ofSeconds(1L);
Duration.ofMillis(1000L);
Duration.ofMillis(1000L);
Duration.between(Instant.ofEpochMilli(1000L), Instant.ofEpochMilli(2000L));
Duration.ofMillis(1000L).toDays();
Duration.ofMillis(1000L).toHours();
Duration.ofMillis(1000L).toMinutes();
Duration.ofMillis(1000L).getSeconds();
Duration.ofMillis(1000L);
Duration.ofMillis(2000L);
Duration.ofMillis(1000L).plusMillis(550L * 2);
Duration.ofMillis(1000L).plus(Duration.ofMillis(550L).multipliedBy(2));
Duration.ofMillis(1000L).plusMillis(550L);
Duration.ofMillis(1000L).plus(Duration.ofMillis(550L));
Duration.ofMillis(1000L).minusMillis(550L);
Duration.ofMillis(1000L).minus(Duration.ofMillis(550L));
Duration.ofMillis(1000L).multipliedBy(2);
Duration.ofMillis(1000L).dividedBy(2);
Duration.ofMillis(1000L).negated();
Duration.ofMillis(1000L).abs();
}
}

Example 19​

JodaFormatterToJavaTimeTest#migrateDateTimeFormat

Before​
import org.joda.time.format.DateTimeFormat;

class A {
public void foo() {
DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTimeFormat.shortDate();
DateTimeFormat.mediumDate();
DateTimeFormat.longDate();
DateTimeFormat.fullDate();
DateTimeFormat.shortTime();
DateTimeFormat.mediumTime();
DateTimeFormat.longTime();
DateTimeFormat.fullTime();
DateTimeFormat.shortDateTime();
DateTimeFormat.mediumDateTime();
DateTimeFormat.longDateTime();
DateTimeFormat.fullDateTime();
}
}
After​
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

class A {
public void foo() {
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);
DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG);
DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.LONG);
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL);
}
}

Example 20​

JodaInstantToJavaTimeTest#migrateInstant

Before​
import org.joda.time.Instant;
import org.joda.time.Duration;

class A {
public void foo() {
new Instant();
Instant.now().getMillis();
Instant.now().minus(Duration.standardDays(1L));
Instant.ofEpochMilli(1234567890L);
Instant.parse("2024-10-25T15:45:00");
Instant.now().plus(Duration.standardDays(1L));
}
}
After​
import java.time.Duration;
import java.time.Instant;

class A {
public void foo() {
Instant.now();
Instant.now().toEpochMilli();
Instant.now().minus(Duration.ofDays(1L));
Instant.ofEpochMilli(1234567890L);
Instant.parse("2024-10-25T15:45:00");
Instant.now().plus(Duration.ofDays(1L));
}
}

Example 21​

JodaIntervalToJavaTimeTest#migrateInterval

Before​
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;
import org.joda.time.DateTimeZone;

class A {
public void foo() {
new Interval(50, 100);
new Interval(50, 100, DateTimeZone.UTC);
new Interval(DateTime.now(), DateTime.now().plusDays(1));
new Interval(DateTime.now(), Duration.standardDays(1));
}
}
After​
import org.threeten.extra.Interval;

import java.time.Duration;
import java.time.Instant;
import java.time.ZonedDateTime;

class A {
public void foo() {
Interval.of(Instant.ofEpochMilli(50), Instant.ofEpochMilli(100));
Interval.of(Instant.ofEpochMilli(50), Instant.ofEpochMilli(100));
Interval.of(ZonedDateTime.now().toInstant(), ZonedDateTime.now().plusDays(1).toInstant());
Interval.of(ZonedDateTime.now().toInstant(), Duration.ofDays(1));
}
}

Example 22​

JodaLocalDateToJavaTimeTest#migrateLocalDate

Before​
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

class A {
public void foo() {
new LocalDate();
new LocalDate(DateTimeZone.UTC);
new LocalDate(2024, 10, 25);
new LocalDate(1234567890L);
new LocalDate(1234567890L, DateTimeZone.UTC);
LocalDate.now().getDayOfWeek();
LocalDate.now().getMonthOfYear();
LocalDate.now().withMonthOfYear(6);
LocalDate.now().plusDays(1);
LocalDate.now().toDateTimeAtStartOfDay();
LocalDate.now().toDateTimeAtStartOfDay(DateTimeZone.UTC);
LocalDate.now().toLocalDateTime(new LocalTime(10, 30));
}
}
After​
import java.time.*;

class A {
public void foo() {
LocalDate.now();
LocalDate.now(ZoneOffset.UTC);
LocalDate.of(2024, 10, 25);
Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()).toLocalDate();
Instant.ofEpochMilli(1234567890L).atZone(ZoneOffset.UTC).toLocalDate();
LocalDate.now().getDayOfWeek().getValue();
LocalDate.now().getMonthValue();
LocalDate.now().withMonth(6);
LocalDate.now().plusDays(1);
LocalDate.now().atStartOfDay(ZoneId.systemDefault());
LocalDate.now().atStartOfDay(ZoneOffset.UTC);
LocalDate.now().atTime(LocalTime.of(10, 30));
}
}

Example 23​

JodaLocalTimeToJavaTimeTest#migrateLocalTime

Before​
import org.joda.time.DateTimeZone;
import org.joda.time.LocalTime;

class A {
public void foo() {
new LocalTime();
new LocalTime(DateTimeZone.UTC);
new LocalTime(10, 30);
new LocalTime(10, 30, 45);
new LocalTime(10, 30, 45, 500);
LocalTime.now().plusMillis(100);
LocalTime.now().minusMillis(100);
LocalTime.now().withMillisOfSecond(500);
LocalTime.now().getMillisOfSecond();
LocalTime.now().getMillisOfDay();
LocalTime.now().getHourOfDay();
LocalTime.now().getMinuteOfHour();
LocalTime.now().getSecondOfMinute();
LocalTime.now().withHourOfDay(10);
LocalTime.now().withMinuteOfHour(30);
LocalTime.now().withSecondOfMinute(45);
LocalTime.now().toDateTimeToday();
LocalTime.now().toDateTimeToday(DateTimeZone.UTC);
}
}
After​
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoField;

class A {
public void foo() {
LocalTime.now();
LocalTime.now(ZoneOffset.UTC);
LocalTime.of(10, 30);
LocalTime.of(10, 30, 45);
LocalTime.of(10, 30, 45, 500 * 1_000_000);
LocalTime.now().plusNanos(100 * 1_000_000L);
LocalTime.now().minusNanos(100 * 1_000_000L);
LocalTime.now().withNano(500 * 1_000_000);
LocalTime.now().get(ChronoField.MILLI_OF_SECOND);
LocalTime.now().get(ChronoField.MILLI_OF_DAY);
LocalTime.now().getHour();
LocalTime.now().getMinute();
LocalTime.now().getSecond();
LocalTime.now().withHour(10);
LocalTime.now().withMinute(30);
LocalTime.now().withSecond(45);
LocalTime.now().atDate(LocalDate.now()).atZone(ZoneId.systemDefault());
LocalTime.now().atDate(LocalDate.now(ZoneOffset.UTC)).atZone(ZoneOffset.UTC);
}
}

Example 24​

JodaTimePeriodToJavaTimeTest#daysBetweenGetDays

Before​
import org.joda.time.DateTime;
import org.joda.time.Days;

class A {
void foo(DateTime start, DateTime end) {
int days = Days.daysBetween(start, end).getDays();
}
}
After​
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

class A {
void foo(ZonedDateTime start, ZonedDateTime end) {
int days = (int) ChronoUnit.DAYS.between(start, end);
}
}

Example 25​

JodaTimeRecipeTest#migrateSafeVariable

Before​
import org.joda.time.DateTime;

class A {
public void foo() {
DateTime dt = new DateTime();
dt.toDateTime().toString();
}
}
After​
import java.time.ZonedDateTime;

class A {
public void foo() {
ZonedDateTime dt = ZonedDateTime.now();
dt.toString();
}
}

Example 26​

NoJodaTimeTest#migrateJodaTime

Before​
import org.joda.time.DateTime;
import org.joda.time.Interval;

class A {
void foo() {
DateTime dt = new DateTime();
DateTime dt1 = new DateTime().plusDays(1);
Interval i = new Interval(dt, dt1);
i.toDuration();
}
}
After​
import org.threeten.extra.Interval;

import java.time.ZonedDateTime;

class A {
void foo() {
ZonedDateTime dt = ZonedDateTime.now();
ZonedDateTime dt1 = ZonedDateTime.now().plusDays(1);
Interval i = Interval.of(dt.toInstant(), dt1.toInstant());
i.toDuration();
}
}
Unchanged​
foo
Before​
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.foobar</groupId>
<artifactId>foobar-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.3</version>
</dependency>
</dependencies>
</project>
After​
pom.xml
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.foobar</groupId>
<artifactId>foobar-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.threeten</groupId>
<artifactId>threeten-extra</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</project>

Usage​

This recipe has no required configuration options. It can be activated by adding a dependency on `org.openrewrite.recipe:rewrite-joda` in your build file or by running a shell command (in which case no build changes are needed):

OpenRewrite artifacts, including the Maven and Gradle plugins themselves, are distributed through the Code Genome Project repository (https://artifacts.codegenomeproject.org/maven), which requires authentication. Sign in to the Code Genome Project to create a download token; your build authenticates with the email or username you signed in with, plus that token as the password. See the quickstart guide for details.

  1. Put your Code Genome Project credentials in ~/.gradle/gradle.properties, so that they are not committed alongside your build:
    ~/.gradle/gradle.properties
    codeGenomeUsername=you@example.com
    codeGenomeToken=your-download-token
  2. Add the Code Genome Project repository to your settings.gradle file, so that the plugin itself can be resolved:
    settings.gradle
    pluginManagement {
    repositories {
    maven {
    url = "https://artifacts.codegenomeproject.org/maven"
    credentials {
    username = providers.gradleProperty("codeGenomeUsername").get()
    password = providers.gradleProperty("codeGenomeToken").get()
    }
    }
    // Keep the portal for any other plugins your build applies
    gradlePluginPortal()
    }
    }
  3. Add the following to your build.gradle file:
    build.gradle
    plugins {
    id("org.openrewrite.rewrite") version("latest.release")
    }

    rewrite {
    activeRecipe("org.openrewrite.java.joda.time.NoJodaTime")
    setExportDatatables(true)
    }

    repositories {
    mavenCentral()
    maven {
    url = "https://artifacts.codegenomeproject.org/maven"
    credentials {
    username = providers.gradleProperty("codeGenomeUsername").get()
    password = providers.gradleProperty("codeGenomeToken").get()
    }
    }
    }

    dependencies {
    rewrite("org.openrewrite.recipe:rewrite-joda:0.11.1")
    }
  4. Run gradle rewriteRun to run the recipe.

See how this recipe works across multiple open-source repositories​

Run this recipe on OSS repos at scale with the Moderne SaaS.

The community edition of the Moderne platform enables you to easily run recipes across thousands of open-source repositories.

Please contact Moderne for more information about safely running the recipes on your own codebase in a private SaaS.

Data Tables​

Source files that had results​

org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

Column NameDescription
Source path before the runThe source path of the file before the run. null when a source file was created during the run.
Source path after the runA recipe may modify the source path. This is the path after the run. null when a source file was deleted during the run.
Parent of the recipe that made changesIn a hierarchical recipe, the parent of the recipe that made a change. Empty if this is the root of a hierarchy or if the recipe is not hierarchical at all.
Recipe that made changesThe specific recipe that made a change.
Estimated time savingAn estimated effort that a developer to fix manually instead of using this recipe, in unit of seconds.
CycleThe recipe cycle in which the change was made.