2
How could I make to return the amount of days to the end of the year, counting on today.
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.format.DateTimeFormatter;
public class TestLocalDate {
public static void main(String[] args) {
//Data de hoje
LocalDate hoje = LocalDate.now();
//ultimo dia do ano 31/12/2016
LocalDate faltaFimDoAno = hoje.with(Month.DECEMBER).withDayOfMonth(31);
System.out.println("Hoje: " + hoje.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))); //saida: Hoje: 28/08/2016
System.out.println("Ultimo dia do ano: " + faltaFimDoAno.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))); //Saída: Ultimo dia do ano: 31/12/2016
System.out.println("Quantidade dias para o fim do ano: " + Period.between(hoje, faltaFimDoAno).getDays()); //Quantidade dias para o fim do ano: 3
}
}
The method Period.between(hoje, faltaFimDoAno).getDays());
Is returning the amount of days and not the total amount counting days, month and year.
Dividing by 30 would not be the solution?
– user28595
That may be. But I wanted to do it with the java.time api
– Gabriel Faria
Do you want the total of days until the end of the year just ne? Without informing months.
– user28595
That’s right, but the . getDays method returns the number of days from day 28 until 31 which are 3
– Gabriel Faria
ok I accepted here =)
– Gabriel Faria