Java 判断是否为当前月份:代码示例及解析
可以使用 Java 中的 Calendar 类来判断是不是当前月。具体实现如下:
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
// 获取当前时间
Calendar calendar = Calendar.getInstance();
// 获取当前月份
int currentMonth = calendar.get(Calendar.MONTH) + 1; // 月份从0开始,需要加1
// 判断是否是当前月
if (currentMonth == 8) { // 假设当前是8月份
System.out.println('是当前月');
} else {
System.out.println('不是当前月');
}
}
}
上面的代码中,先获取当前时间,然后使用 Calendar 类的 get 方法获取当前月份,最后与指定月份进行比较,判断是否是当前月。
原文地址: https://www.cveoy.top/t/topic/nf8t 著作权归作者所有。请勿转载和采集!