2

我正在尝试创建一个程序,它接受两个日期作为参数,并返回日期是否在本周,但我在调用isDateInCurrentWeek. 当我尝试编译程序时,我收到一条错误消息

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;
   }
} 

不太确定问题是什么,因为该方法将公历作为输入,并且两个日期采用公历格式。有人知道吗?

4

2 回答 2

2

你有一个;在末尾if,删除它...

公历是日历,而不是相反。如果抽象和通用方法适合您的需要,请更改签名以使用日历。如“编程到接口”意味着什么?(感谢路易吉)。

hols id 不是一个好的班级名称。霍尔斯可能会更好...

此代码运行良好:

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;
   }
}
于 2013-02-19T20:05:16.930 回答
1

当您调用isDateInCurrentWeek时,您传入的变量会被输入Calendar。将它们声明为GregorianCalendar

或者,如果您不需要任何GregorianCalendar特定内容,请更改方法签名isDateInCurrentWeek以接受Calendar.

于 2013-02-19T19:57:30.500 回答