Reputation: 77
i have 2 Date (start_date
, end_date
) with yyyy-MM-dd format and i want to check that if the month between in december 01 - march 31 then do something. For example my start_date
is 2015-12-01 or 2016-02-01 and end_date
2016-02-12 then write something.
I have this
public void meethod(){
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date startDate = null;
Date endDate = null;
try {
startDate = df.parse(tf_start_date.getText());
endDate = df.parse(tf_end_date.getText());
} catch (ParseException e) {
e.printStackTrace();
}
if( /* what goes here? */ ) {
System.out.println("its between december-march");
} else {
}
}
tf_start_date
and tf_end_date
is a TextField and the value of TextFields like 2015-02-03
Upvotes: 0
Views: 62
Reputation: 4945
First, you should probably be using Calendar
rather than Date
. First, you'll need to construct the Calendar
using a Calendar.Builder
:
Calendar startCal = Calendar.Builder().setInstant(startDate);
Calendar endCal = Calendar.Builder().setInstant(endDate);
Then you can just check their months to see if they are one of the months you're looking for:
int startMonth = startCal.get(Calendar.MONTH);
if (startMonth == Calendar.DECEMBER ||
startMonth == Calendar.JANUARY ||
startMonth == Calendar.FEBRUARY ||
startMonth == Calendar.MARCH)
Similarly with endMonth
.
Upvotes: 2