Convert day of year to day of month in Java

Java 8Object Oriented ProgrammingProgramming

Firstly, set the day of year using DAY_OF_YEAR constant.

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2018);
cal.set(Calendar.DAY_OF_YEAR, 320);

Now, get the day of month −

int res = cal.get(Calendar.DAY_OF_MONTH);

The following is an example −

Example

 Live Demo

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.YEAR, 2018);
      cal.set(Calendar.DAY_OF_YEAR, 320);
      System.out.println("Date = " + cal.getTime());
      int res = cal.get(Calendar.DAY_OF_MONTH);
      System.out.println("Day of month = " + res);
   }
}

Output

Date = Fri Nov 16 07:54:55 UTC 2018
Day of month = 16
raja
Published on 19-Dec-2018 18:09:34
Advertisements