Days till End of Year in Java


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

Firstly, calculate the day of the 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);

Find the difference and you will get the days until the end of the year.

The following is the complete example.

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);
      System.out.println("Current Year: "+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 "+year);
   }
}

Output

Current Year: 2018
Days in current year: 365
Total 38 days remaining in 2018

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 29-Jun-2020

396 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements