5

I am currently ditching all usages of java.util Date implementations from my project and want to change everything to Classes from the java.time package.

Now I found a usage of the roll method of a java.util.Calendar object and I am wondering how I can change this to something from the java.time package.

I checked if the date classes of java.time have a roll method, but I could not find any.

    ZonedDateTime.now().roll(); // method does not exist
    LocalDateTime.now().roll(); // method does not exist
    LocalDate.now().roll(); // method does not exist

I'm looking for something from the java.time package that does the same as the roll method from the java.util.Calendar class.

Somebody already asked a similiar question: What is the equivalent of Calendar.roll in java.time? But I find the accepted answer unsatisfactory. It does not answer whether there is something similar to roll in java.time or not.

zingi
  • 1,141
  • 13
  • 23
  • In fact, the question is similar. But I find the accepted answer unsatisfactory. It does not answer whether there is something similar to roll in java.time or not. – zingi Aug 02 '19 at 12:44
  • 3
    No, there isn't. – JB Nizet Aug 02 '19 at 12:45
  • 3
    The first thing I would do is check whether the `Calendar.roll` method was being used correctly. There are very few legitimate uses in the real world. – Klitos Kyriacou Aug 02 '19 at 12:50
  • 1
    @KlitosKyriacou I talked now with a coworker about this specific code where roll was used. And he said that they only used roll, because of the absence of a .minus() method. So they only used it to subtract time _without the actual need of the behaviour of roll_, which doesn't change larger fields. – zingi Aug 02 '19 at 13:06
  • 2
    @zingi it's fully documented in the Javadoc that to subtract from a Calendar you use `add()` with a negative argument. So they didn't have a need to use `roll()` at all. – Klitos Kyriacou Aug 02 '19 at 13:33
  • @KlitosKyriacou I know... I didn‘t write this piece of code. – zingi Aug 02 '19 at 13:40

0 Answers0