How do I discover the Quarter of a given Date in Java?


Let us first get the current date −

LocalDate currentDate = LocalDate.now();

Now, use the Calendar class and set the locale −

Calendar cal = Calendar.getInstance(Locale.US);

Now, get the month −

int month = cal.get(Calendar.MONTH);

Find the Quarter −

int quarter = (month / 3) + 1;

Example

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);
   }
}

Output

Current Date = 2019-04-12
Quarter = 2

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements