gpt4 book ai didi

java - 提取一年中特定星期几的所有日期 - Java

转载 作者:搜寻专家 更新时间:2023-11-01 03:22:40 24 4
gpt4 key购买 nike

已经为这个问题苦苦挣扎了一段时间,希望得到一些意见。

我要解决的问题是收集一个特定年份中属于一周中特定日期的所有日期,例如,2014 年的每个星期二。日期存储在 ArrayList<Date> 中。 .然后返回此列表。

还必须验证以确保年份不为 0,并且提交的星期几必须是 1-7 之间的数字。

如果有任何问题,我很想知道我搞砸了什么。

public List<Date> getDatesforDayOfWeek(int year, int dayOfWeek) throws InvalidDateException, ParseException {

List<Date> dateList = new ArrayList<>();

if (year <= 0 || (1 > dayOfWeek && dayOfWeek > 7)) {

throw new InvalidDateException("Year or day of week is invalid.");

} else {

Calendar newCal = Calendar.getInstance();
newCal.set(YEAR, year);
newCal.set(DAY_OF_YEAR, 1);



while (newCal.get(YEAR) < year + 1) {

int currentDayOfWeek = newCal.get(DAY_OF_WEEK);

Date newDate = null;
if (currentDayOfWeek >= dayOfWeek) {

int dayOfMonth = newCal.get(DAY_OF_MONTH);
String strDayOfMonth = String.valueOf(dayOfMonth);
String strYear = String.valueOf(year);

DateUtility d1 = new DateUtility();
Date passDate = newCal.getTime();

String weekDay = d1.getWeekDayNameAbbreviation(passDate);
String monthAbbreviation = d1.getMonthAbbreviation(passDate);


String finalString = new String();

finalString.concat(weekDay).concat(" ").
concat(monthAbbreviation).concat(" ").
concat(strDayOfMonth).concat(" ").
concat(strYear);

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd YYYY");
Date theDate = format.parse(finalString);

dateList.add(theDate);
}
newCal.add(Calendar.DATE, 1);
}

}
return (dateList);
}

最佳答案

您的问题未能指定哪一天是一周的第一天,但​​是您测试一周中当前日期的方法使事情变得更加复杂。让我们从使用 Calendar 标准验证星期几开始,

private static boolean isValidDayOfWeek(int dayOfWeek) {
switch (dayOfWeek) {
// Seven days of the week.
case Calendar.SUNDAY: case Calendar.MONDAY: case Calendar.TUESDAY:
case Calendar.WEDNESDAY: case Calendar.THURSDAY: case Calendar.FRIDAY:
case Calendar.SATURDAY:
return true;
}
return false;
}

接下来我们可以做类似的事情,

public static List<Date> getDatesforDayOfWeek(int year, int dayOfWeek) {
List<Date> dateList = new ArrayList<>();
if (year <= 0 || !isValidDayOfWeek(dayOfWeek)) {
return null;
} else {
Calendar newCal = Calendar.getInstance();
newCal.set(Calendar.YEAR, year);
newCal.set(Calendar.DAY_OF_YEAR, 1);
// First, let's loop until we're at the correct day of the week.
while (newCal.get(Calendar.DAY_OF_WEEK) != dayOfWeek) {
newCal.add(Calendar.DAY_OF_MONTH, 1);
}
// Now, add the Date to the List. Then add a week and loop (stop
// when the year changes).
do {
dateList.add(newCal.getTime());
newCal.add(Calendar.DAY_OF_MONTH, 7);
} while (newCal.get(Calendar.YEAR) == year);
}
return dateList;
}

留给我们 main()。因此,要获取 2014 年的每个星期二,您可以使用 -

public static void main(String[] args) {
List<Date> tuesdays = getDatesforDayOfWeek(2014, Calendar.TUESDAY);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
for (Date d : tuesdays) {
System.out.println(df.format(d));
}
}

关于java - 提取一年中特定星期几的所有日期 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25650126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com