首先,使用DAY_OF_YEAR常量设置一年中的某天。
Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, 2018); cal.set(Calendar.DAY_OF_YEAR, 320);
现在,获取月中的某天-
int res = cal.get(Calendar.DAY_OF_MONTH);
以下是一个例子-
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); } }
输出结果
Date = Fri Nov 16 07:54:55 UTC 2018 Day of month = 16