Java中可以使用Calendar类来获取当前月份的天数。具体步骤如下:
import java.util.Calendar;
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
完整的代码示例:
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int month = calendar.get(Calendar.MONTH) + 1;
int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("当前月份:" + month);
System.out.println("当前月份的天数:" + days);
}
}
运行以上代码,会输出当前月份和当前月份的天数。