1

I'm trying to add 2 times like so: I have a LocalTime called from(da) and I want to make it so that "from" is the start parameter (inizio) with some minutes added (tariffa.getMinutiFranchigia(), that I'm getting from another class). How can I do this using LocalTime and Duration? I can't change the type of getMinutiFranchigia() to TemporalAmount.

public Ticket emettiTicket(LocalTime inizio, LocalTime fine) {
        LocalTime da;
        LocalTime a;
        if (tariffa.getDurataMinima() == 0 && tariffa.getMinutiFranchigia() == 0) {
            da = inizio;
            a = fine;
        } 
        else if (tariffa.getDurataMinima() == 0 && tariffa.getMinutiFranchigia() != 0) {
            int franchigia = tariffa.getMinutiFranchigia();
            da = inizio + Duration.of(0, tariffa.getMinutiFranchigia());    
            }//calcola costo e restituisci ticket
    }
chiagger
  • 63
  • 1
  • 8
  • You seem to be doing this correctly for the most part. You just need to call `inizio.plus` rather than using the `+` operator. – Sweeper Mar 24 '21 at 10:47
  • 2
    You could simply do `da = inizio.plusMinutes(tariffa.getMinutiFranchigia())` if I guessed right by assuming that method returns an `int` amount of minutes. – deHaar Mar 24 '21 at 10:48

1 Answers1

2

You can add an amount of minutes to a LocalTime da by simply using a certain method of LocalTime: plusMinutes():

da = inizio.plusMinutes(tariffa.getMinutiFranchigia())
deHaar
  • 17,687
  • 10
  • 38
  • 51
chiagger
  • 63
  • 1
  • 8