How to convert days into human-readable years, months and days in Java

3 Answers

0 votes
import java.time.LocalDate;
import java.time.Period;

public class DateDifference {
    public static void main(String[] args) {
        // Difference between Jan 1, 2024 and Mar 27, 2025 (including both days):
        // 1 years 2 months 27 days
        // or 14 months 27 days
        // or 64 weeks 4 days
        // or 452 calendar days

        int days = 452;
        LocalDate startDate = LocalDate.of(2024, 1, 1); // based on actual days in each month
        LocalDate endDate = startDate.plusDays(days);

        Period period = Period.between(startDate, endDate);
        System.out.println(period.getYears() + " years " + 
                           period.getMonths() + " months " + 
                           period.getDays() + " days");
    }
}



/*
run:

1 years 2 months 27 days
  
*/

 



answered Jun 27, 2024 by avibootz
edited Dec 31, 2025 by avibootz
0 votes
import java.time.LocalDate;
import java.time.Period;

public class DaysConverter {

    public static Period splitDays(int totalDays) {
        LocalDate start = LocalDate.of(1970, 1, 1);
        LocalDate end   = start.plusDays(totalDays);
        
        return Period.between(start, end);
    }

    public static void main(String[] args) {
        Period p = splitDays(452);
        
        System.out.println(p.getYears() + " years, " +
                           p.getMonths() + " months, " +
                           p.getDays() + " days");
    }
}



/*
run:

1 years, 2 months, 28 days
  
*/

 



answered Dec 31, 2025 by avibootz
0 votes
public class DaysConverter {

    public static int[] splitDaysSimple(int totalDays) {
        int years  = totalDays / 365;
        totalDays %= 365;

        int months = totalDays / 30;
        totalDays %= 30;

        int days   = totalDays;

        return new int[] { years, months, days };
    }

    public static void main(String[] args) {
        int[] result = splitDaysSimple(452);
        System.out.println(result[0] + " years, " +
                           result[1] + " months, " +
                           result[2] + " days");
    }
}



/*
run:

1 years, 2 months, 27 days
  
*/

 



answered Dec 31, 2025 by avibootz

Related questions

...