在Java中,可以使用java.time.YearMonth
类来获取当月的天数。
import java.time.YearMonth;
public class Main {
public static void main(String[] args) {
YearMonth yearMonth = YearMonth.now();
int daysInMonth = yearMonth.lengthOfMonth();
System.out.println("当前月份的天数:" + daysInMonth);
}
}
以上代码中,首先创建了一个YearMonth
对象表示当前月份,然后使用lengthOfMonth()
方法获取当月的天数。最后打印出当月的天数。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java怎么获取当月天数