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);
}
}
運行以上代碼,會輸出當前月份和當前月份的天數。