w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Get the months remaining in the year

Secondary Nav

Java DateTime, Calendar : Exercise-23 with Solution

Write a Java program to get the months remaining in the year.

Java Code:

import java.time.*;
import java.time.temporal.TemporalAdjusters;
public class Exercise23 {
   public static void main(String[] args)
    {
     LocalDate today = LocalDate.now(); 
     LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
     Period period = today.until(lastDayOfYear);
     System.out.println();
     System.out.println("Months remaining in the year: "+period.getMonths());	
     System.out.println();
   }
}

Sample Output :

Months remaining in the year: 3

Java Practice online:

import java.time.*;
import java.time.temporal.TemporalAdjusters;
public class Exercise23 {
   public static void main(String[] args)
    {
     LocalDate today = LocalDate.now(); 
     LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
     Period period = today.until(lastDayOfYear);
     System.out.println();
     System.out.println("Months remaining in the year: "+period.getMonths());	
     System.out.println();
   }
}



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program