Question

I'm trying to create a program which takes two dates as arguments, and returns whether the dates are in the current week or not, but I am having an issue with the call to isDateInCurrentWeek. When I try to compile the program, I receive an error saying

requestHoliday.java:16: isDateInCurrentWeek(java.util.GregorianCalendar) in requestHoliday cannot be applied to (java.util.Calendar)
      if (isDateInCurrentWeek(startDate) && isDateInCurrentWeek(endDate));

.

import java.util.*;
import java.text.*;

public class hols {
   public static void main( String[] args ) {
      DateFormat df = new SimpleDateFormat("dd MM yyyy");
      Date start = df.parse(args[0]);
      Date end = df.parse(args[1]);
      Calendar startDate = new GregorianCalendar();
      Calendar endDate = new GregorianCalendar();
      startDate.setTime(start);
      endDate.setTime(end);
      if( isDateInCurrentWeek( startDate ) && isDateInCurrentWeek( endDate ));
         System.out.println( "Date is in current week!" );
   }

   public static boolean isDateInCurrentWeek(GregorianCalendar date) {
      Calendar currentCalendar = GregorianCalendar.getInstance();
      int week = currentCalendar.get(Calendar.WEEK_OF_YEAR);
      int year = currentCalendar.get(Calendar.YEAR);
      Calendar targetCalendar = GregorianCalendar.getInstance();
      targetCalendar = date;
      int targetWeek = targetCalendar.get(Calendar.WEEK_OF_YEAR);
      int targetYear = targetCalendar.get(Calendar.YEAR);
      return week == targetWeek && year == targetYear;
   }
} 

Not too sure what the issue is, as the method takes a Gregoriancalendar as input, and the two dates are in Gregorian Calendar format. Anybody know?

Was it helpful?

Solution

You have a ; at the end of if, remove it...

Gregorian calendar is a Calendar, not the opposite. Change the signature to use Calendar if abstract and generic methods are adapted to your needs. When possible is better to use contracts than implementations as explained in What does it mean to "program to an interface"? (thanks to Luiggi).

hols id not a good class name. Hols may be better...

This code works well:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Hols {
   public static void main( String[] args ) throws ParseException {
      DateFormat df = new SimpleDateFormat("dd MM yyyy");
      Date start = df.parse( args[0] );
      Date end   = df.parse( args[1] );
      Calendar startDate = new GregorianCalendar();
      Calendar endDate = new GregorianCalendar();
      startDate.setTime(start);
      endDate.setTime(end);
      if( isDateInCurrentWeek( startDate ) && isDateInCurrentWeek( endDate ))
         System.out.println( "Date is in current week!" );
      else
         System.out.println( "Date is NOT in current week!" );
   }

   public static boolean isDateInCurrentWeek( Calendar date ) {
      Calendar currentCalendar = Calendar.getInstance();
      int week = currentCalendar.get(Calendar.WEEK_OF_YEAR);
      int year = currentCalendar.get(Calendar.YEAR);
      Calendar targetCalendar = Calendar.getInstance();
      targetCalendar = date;
      int targetWeek = targetCalendar.get(Calendar.WEEK_OF_YEAR);
      int targetYear = targetCalendar.get(Calendar.YEAR);
      return week == targetWeek && year == targetYear;
   }
}

OTHER TIPS

When you call isDateInCurrentWeek, the variables you pass in are typed Calendar. Declare them as GregorianCalendar instead.

Or if you don't need anything GregorianCalendar-specific, change the method signature if isDateInCurrentWeek to accept a Calendar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top