Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements