[Solved] Java: I can add as period to another period?


Java is a powerful and versatile programming language that is used to create a wide variety of applications. It is an object-oriented language that allows developers to create complex programs with ease. One of the most common questions asked by Java developers is whether they can add a period to another period. The answer is yes, and this article will explain how to do it.


No, you cannot add one period to another period. A period is a specific length of time, and it cannot be extended or shortened.

Before begining your loop create three variables wich are totalAnios (totalYears), totalMeses (totalMonths) , totalDias (totalDays) and initialize them to 0.

   int totalAnios = 0;
   int totalMeses = 0;
   int totalDias = 0; 
   do {

And before closing the loop (before while(…) ) you have to add the difference of years, months and days to the previous varibales you created ( totalAnios , totalMeses , totalDias ) and print the result.

do {
 /* your calculation goes here */

   totalAnios += anios;
   totalMeses += meses;
   totalDias  += dias;

            System.out.println("total : " + totalAnios + " " + totalMeses + " " + totalDias);
} while (seguir != 'n');

Each time the user choose to stay in the loop, we sum the previous results and print the total.

By the way, next time don’t forget to format your code and put variables in english ? .


solved Java: I can add as period to another period?

public class PeriodAdd {
    public static void main(String[] args) {
        Period period1 = Period.of(1, 2, 3);
        Period period2 = Period.of(4, 5, 6);
        Period period3 = period1.plus(period2);

The above code demonstrates how to add two Period objects in Java. The Period class is part of the java.time package and is used to represent a period of time. In this example, two Period objects are created and then added together using the plus() method. The result is a new Period object which is then printed to the console. The output of this code is “P5Y7M9D”, which represents a period of 5 years, 7 months, and 9 days.