Skip to main content
Open source

Prefer the Java standard library instead of Joda-Time

Recipe IDorg.openrewrite.java.joda.time.NoJodaTime
Artifactorg.openrewrite.recipe:rewrite-joda

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.

Composite recipeJavajoda-timeModerne Source Available License
Try in PlatformTry this recipe in the Moderne platform. Not a user yet? You’ll get a no-setup demo environment, with nothing to install or configure.

Examples

java
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"));
}
}
java
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()));
}
}
java
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());
}
}
java
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();
}
}
java
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();
}
}
java
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);
}
}
java
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));
}
}
java
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));
}
}
java
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));
}
}
java
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);
}
}
java
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);
}
}
java
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();
}
}
java, xml
Unchanged
foo
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();
}
}
Before
<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
<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>
java
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"));
}
}
java
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()));
}
}
java
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());
}
}
java
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();
}
}
java
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();
}
}
java
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);
}
}
java
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));
}
}
java
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));
}
}
java
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));
}
}
java
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);
}
}
java
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);
}
}
java
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();
}
}
java, xml
Unchanged
foo
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();
}
}
Before
<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
<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

Run this recipe

This recipe has no required configuration options. Users of Moderne can run it via the Moderne CLI.

You will need to have configured the Moderne CLI on your machine before you can run the following command.

shell
mod run . --recipe NoJodaTime

If the recipe is not available locally, then you can install it using:

mod config recipes jar install org.openrewrite.recipe:rewrite-joda:0.9.1

Data tables

Source files that had results
org.openrewrite.table.SourcesFileResults

Source files that were modified by the recipe run.

ColumnDescription
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.
Source files that had search results
org.openrewrite.table.SearchResults

Search results that were found during the recipe run.

ColumnDescription
Source path of search result before the runThe source path of the file with the search result markers present.
Source path of search result after run 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.
ResultThe trimmed printed tree of the LST element that the marker is attached to.
DescriptionThe content of the description of the marker.
Recipe that added the search markerThe specific recipe that added the Search marker.
Source files that errored on a recipe
org.openrewrite.table.SourcesFileErrors

The details of all errors produced by a recipe run.

ColumnDescription
Source pathThe file that failed to parse.
Recipe that made changesThe specific recipe that made a change.
Stack traceThe stack trace of the failure.
Recipe performance
org.openrewrite.table.RecipeRunStats

Statistics used in analyzing the performance of recipes.

ColumnDescription
The recipeThe recipe whose stats are being measured both individually and cumulatively.
Source file countThe number of source files the recipe ran over.
Source file changed countThe number of source files which were changed in the recipe run. Includes files created, deleted, and edited.
Cumulative scanning time (ns)The total time spent across the scanning phase of this recipe.
Max scanning time (ns)The max time scanning any one source file.
Cumulative edit time (ns)The total time spent across the editing phase of this recipe.
Max edit time (ns)The max time editing any one source file.