I need a java logic to check if a certain date(lets assume 18-04-2017) falls in the current week or not. Also We have 4 quarters in an year (Jan, Feb, Mar) - 1Q (Apr, May, june) - 2Q So i need to know if this same date falls in the current Quarter or not. Will appreciate any replies
Asked
Active
Viewed 689 times
-8
-
2Have you tried anything? – tnw Apr 18 '17 at 14:51
-
Should not have been closed as "too broad" as it is quite specific. But it should be closed as a duplicate of many Questions on quarters and many Questions on testing if a date lies within a range. Tip: `org.threeten.extra.YearQuarter::atDay` and `::atEndOfQuarter`. – Basil Bourque Apr 18 '17 at 19:05
1 Answers
3
I would say do these steps,
1 - figure out first and last day of the week Get current week start and end date in Java - (MONDAY TO SUNDAY)
2 - check if the date falls in between that range or not Java: how do I check if a Date is within a certain range? - if yes, it falls in the week - if no, it doesn't
3 - do the same for the quarters, have start and end date, and check if the date falls in between them or not
//import java.util.*;
//import java.text.*;
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date dateToTest = sdf.parse("18/04/2017");
//lets assume the date source is a variable, hence extract year off of it
String firstDateInString = "01/04/" + Integer.toString(getYear(dateToTest));
String secondDateInString = "01/07/" + Integer.toString(getYear(dateToTest));
String thirdDateInString = "01/10/" + Integer.toString(getYear(dateToTest));
Date firstDate = sdf.parse(firstDateInString);
Date secondDate = sdf.parse(secondDateInString);
Date thirdDate = sdf.parse(thirdDateInString);
//check if in first quarter
//if date is before April it has to be first Q
if (dateToTest.before(firstDate)) {
System.out.println("First Quarter");
return;
}
//check if in second quarter
//if date is before August it has to be first Q
if (dateToTest.before(secondDate)) {
System.out.println("Second Quarter");
return;
}
//check if in 3rd quarter
//if date is before August it has to be first Q
if (dateToTest.before(thirdDate)) {
System.out.println("third Quarter");
return;
}
//well then the date must be in 4th quarter
System.out.println("Fourth quarter");
} catch (Exception err) {
System.out.println(err);
}
}
private static int getYear(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
DISCLAIMER: This code is not the optimal method. Please note that here I don't assume current date or year. Purpose here is to show general strategy only.

Community
- 1
- 1

Samip Suwal
- 1,253
- 11
- 17
-
thanks..Implemented it for Week..Now need to figure out a way for the quarters... – shahed Apr 18 '17 at 21:21
-
hi @shahed please check updated answer for a sample. Please note that the sample doesn't answer your question directly. You will still need to do some work to check for current year's quarters. I hope it helps :) – Samip Suwal Apr 18 '17 at 23:32
-
I think this serves the purpose..thanks a lot Samip Sir.. I ll try will give u the feedback – shahed Apr 19 '17 at 07:21