0

我有以下代码:

DateFormat df = new SimpleDateFormat("EEEEE, LLLL d", Locale.US);
String[] suspendedDates = {
        "Tuesday, January 1",
        "Monday, January 21",
        "Sunday, February 10",
        "Tuesday, February 12",
        "Wednesday, February 13",
        "Monday, February 18",
        "Sunday, February 24",
        "Tuesday, March 26",
        "Wednesday, March 27",
        "Thursday, March 28",
        "Friday, March 29",
        "Monday, April 1",
        "Tuesday, April 2",
        "Thursday, May 2",
        "Friday, May 3",
        "Thursday, May 9",
        "Wednesday, May 15",
        "Thursday, May 16",
        "Monday, May 27",
        "Thursday, July 4",
        "Wednesday, August 7",
        "Thursday, August 8",
        "Friday, August 9",
        "Thursday, August 15",
        "Monday, September 2",
        "Thursday, September 5",
        "Friday, September 6",
        "Saturday, September 14",
        "Thursday, September 19",
        "Friday, September 20",
        "Thursday, September 26",
        "Friday, September 27",
        "Monday, October 14",
        "Tuesday, October 15",
        "Wednesday, October 16",
        "Thursday, October 17",
        "Friday, November 1",
        "Sunday, November 3",
        "Tuesday, November 5",
        "Monday, November 11",
        "Thursday, November 28",
        "Sunday, December 8",
        "Wednesday, December 25"
};

如果匹配,则以下行进行比较并返回 true:

Arrays.asList(suspendedDates).contains(df.format(Calendar.getInstance(Locale.US).getTime()))

我想知道的是,如何检查它匹配的索引?

4

1 回答 1

3

您可以为此使用界面中的indexOf方法。List既然Arrays.asList(suspendedDates)给了你一个List你可以使用这个代码:

int index = Arrays.asList(suspendedDates).indexOf(...)

-1如果没有找到,这将返回。

作为旁注,请考虑使用Date类而不是将日期存储在字符串中。

于 2013-09-29T23:16:40.077 回答