0

I need to implement the logic whether input date such as SEP 2014, OCT 2014 , NOV 2014 etc is current month or not. I have written below logic for checking whether a selected week is a current week or not using below method but not able to implement for current month

public static boolean isCurrentWeek(Date date, Date dateStart, Date dateEnd)

 {

    if (date != null && dateStart != null && dateEnd != null) {


        if(date.equals(dateStart) || date.equals(dateEnd) ){


            return true;


        }else if (date.after(dateStart) && date.before(dateEnd)) {


            return true;


        }


        else {


            return false;

        }

    }
    return false;
}
  • 1
    You have code for current week, and you're trying to figure out a current month version, what have you tried so far? Post *that* code. – rolfl Oct 03 '14 at 14:05

2 Answers2

1

Try Apache Commons for this:

DateUtils.truncatedEquals( date, new Date(), Calendar.MONTH );

It returns true if the two date is in the same month.

EDIT

Maybe this:

public static boolean isInCurrentMonth(String inputDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
    try {
        Date inputDate = sdf.parse(inputDateString);
        return DateUtils.truncatedEquals(new Date(), inputDate, Calendar.MONTH);
    } catch (ParseException e) {
        // error handling
    }
}
Krayo
  • 2,492
  • 4
  • 27
  • 45
  • I have two date on my calender, startDate="OCT 2014" and end Date is also the same, so i am getting this value in String but not able to figure it out how to to get First day and last of the month selected – feroz siddiqui Oct 03 '14 at 14:14
  • Basicaly i need to check whether today date is in between the start date and end date of a month. but as i am getting a string OCT 2014 as start Date and End date i am not able to parse it to date format – feroz siddiqui Oct 03 '14 at 14:16
0

I think you can solve your problem with code similar to this one:

String target = "Oct 2000";
DateFormat df = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH);
try{
  Date date = df.parse(target);
  Calendar calendar = Calendar.getInstance();
  Calendar targetCalendar = Calendar.getInstance();
  targetCalendar.setTime(date);
  System.out.println(targetCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH));
 } catch (ParseException e){
 }

String target is your input like "Sep 2014". This string is parsed to a Date and then used to set the time in a Calendar object. The second Calendar contains the current time. The check if boths calenders match on their month is done is the System.out.println call.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
  • Stefan, your code is checking only month but not year for e.g if i pass String target = "Oct 2000" and current month is OCT 2014 its will return true. It should compare the year as well – feroz siddiqui Oct 03 '14 at 14:38
  • I have modified the your as below and its working fine public static boolean isCurrentMonth(String dateStart) throws ParseException{ DateFormat df = new SimpleDateFormat("MMM yyyy", Locale.ENGLISH); Date date = df.parse(dateStart); Calendar calendar = Calendar.getInstance(); Calendar targetCalendar = Calendar.getInstance(); targetCalendar.setTime(date); return (targetCalendar.get(Calendar.MONTH) == calendar.get(Calendar.MONTH) && targetCalendar.get(Calendar.YEAR) == calendar.get(Calendar.YEAR)); } – feroz siddiqui Oct 03 '14 at 14:41
  • In your question you are asking only for a match on the month (you wrote: "is current month or not."). As you already know, the code can be easily extended to check also for the year :) So, I you think this solutions works for you, please vote it up. – Stefan Freitag Oct 03 '14 at 14:48