Get the days remaining in current year in Java

To get the days remaining in the current year, find the difference between the total days in the current year with the total number of days passed.

First, calculate the day of year.

Calendar calOne = Calendar.getInstance();
int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);

Now, calculate the total days in the current year 2018.

int year = calOne.get(Calendar.YEAR);
Calendar calTwo = new GregorianCalendar(year, 11, 31);
int day = calTwo.get(Calendar.DAY_OF_YEAR);
System.out.println("Days in current year: "+day);

The following is the final example that finds the difference between the above two to get the days remaining in the current year.

Example

Live Demo

import java.util.Calendar;
import java.util.GregorianCalendar;
public class Demo {
public static void main(String args[]) {
Calendar calOne = Calendar.getInstance();
int dayOfYear = calOne.get(Calendar.DAY_OF_YEAR);
int year = calOne.get(Calendar.YEAR);
Calendar calTwo = new GregorianCalendar(year, 11, 31);
int day = calTwo.get(Calendar.DAY_OF_YEAR);
System.out.println("Days in current year: "+day);
int total_days = day - dayOfYear;
System.out.println("Total " + total_days + " days remaining in current year");
}
}

Output

Days in current year: 365
Total 38 days remaining in current year
Samual Sam

Learning faster. Every day.