Convert day of year to day of month in Java


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 29-Jun-2020

435 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements