w3resource logo


Java Programming Exercies

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

Java DateTime, Calendar : Exercise-23 with Solution

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

Sample Solution:

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("Months remaining in the year: "+period.getMonths());	

Sample Output:

Months remaining in the year: 6 

N.B.: The result may varry for your system date and time.

Java Code Editor:

Improve this sample solution and post your code through Disqus