如何在Java中发现给定日期的四分之一?
我们首先获取当前日期-
LocalDate currentDate = LocalDate.now();
现在,使用Calendar类并设置语言环境-
Calendar cal = Calendar.getInstance(Locale.US);
现在,获取月份-
int month = cal.get(Calendar.MONTH);
找到四分之一-
int quarter = (month / 3) + 1;
示例
import java.time.LocalDate; import java.util.Calendar; import java.util.Locale; public class Demo { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println("Current Date = "+currentDate); Calendar cal = Calendar.getInstance(Locale.US); int month = cal.get(Calendar.MONTH); int quarter = (month / 3) + 1; System.out.println("Quarter = "+quarter); } }
输出结果
Current Date = 2019-04-12 Quarter = 2