Return number of days for the end of the year

Asked

Viewed 514 times

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?

  • That may be. But I wanted to do it with the java.time api

  • Do you want the total of days until the end of the year just ne? Without informing months.

  • That’s right, but the . getDays method returns the number of days from day 28 until 31 which are 3

  • 1

    ok I accepted here =)

3 answers

3

Use ChronoUnit to make that comparison:

//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"))); 

System.out.println("Ultimo dia do ano: " + faltaFimDoAno.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")));

System.out.println("Quantidade dias para o fim do ano: " + ChronoUnit.DAYS.between(hoje, faltaFimDoAno));

Exit:

Today: 28/08/2016
Last day of the year: 31/12/2016
Quantity days for end of year: 125

See on ideone.

  • Arrived first for a half dozen seconds hehehe :)

  • @Anthonyaccioly already had idea of the answer, I just thought he wanted a full description as "Missing x months and x days to the end of the year". Then I wouldn’t know how to do it for now.

  • 1

    Rolled with Period.between, from there you call getDays, getMonths, etc. Anyway that package temporal is well hidden. The only difference between my answer and yours was that I was using LocalDate.now().with(TemporalAdjuster.lastDayOfYear()), another hidden gem.

3


Another alternative is to use the function toDays of Duration.between:

import static java.time.temporal.TemporalAdjusters.lastDayOfYear;
//...

LocalDate hoje = LocalDate.now();
LocalDate faltaFimDoAno = hoje.with(lastDayOfYear());

long dias = Duration.between(hoje.atStartOfDay(), faltaFimDoAno.atStartOfDay()).toDays();

System.out.println("Quantidade de dias para o fim do ano: " + dias);

See DEMO

1

If you want a solution using JAVA 1.8+ , you can use the Chronounit, otherwise you can also do this way below using Calendar and Gregoriancalendar compatible with lower versions:

Calendar data1 = Calendar.getInstance();
Calendar data2 = new GregorianCalendar(data1.get(Calendar.YEAR), 11, 31);

int diasRestantes = data2.get(Calendar.DAY_OF_YEAR) - data1.get(Calendar.DAY_OF_YEAR);
System.out.println("Restam "diasRestantes + " dias para acabar o ano!");

Good Luck!

  • In accordance with this answer it is not a good idea to use these classes more for time comparison.

  • @diegofm I’ll give a deeper search on the subject. No problem has ever happened to me until today, but I believe it is worth reading a little more about! Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.