Search code examples
javatimedurationlocaltime

adding a duration to a LocalTime


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
    }

Solution

  • 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())