如何在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP