作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
已经为这个问题苦苦挣扎了一段时间,希望得到一些意见。
我要解决的问题是收集一个特定年份中属于一周中特定日期的所有日期,例如,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/
本文实例为大家分享了php实现按天数、星期、月份查询的搜索框,搜索时候展示数据的统计图,主要展示图形的效果,供大家参考,具体内容如下 1.ajax.php ?
星期-时间时刻散点平面坐标系统计大小量,Python,matplotlib,散点图(1) 假设现在有一堆年月日时数据,形如t=2022-07-31 23:45:12,每一条这样的时刻都对应发生了一次事
我想以 EEEE d, MMMM 格式自定义此显示日、日期和月份格式。 提前致谢。 最佳答案 这是完全合乎逻辑的。你可以试试这个 public static class EndDatePickerFr
我是一名优秀的程序员,十分优秀!