java 怎么算出今年所有的双休日
发布网友
发布时间:2023-07-10 14:22
我来回答
共1个回答
热心网友
时间:2024-12-04 22:14
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
public class WeekDays {
public static void main(String[] args) {
List<String> weekenDays = getWeekends(2011);
int count = 1;
for(String weekDay: weekenDays){
System.out.print(weekDay + " ");
if(count++ == 7){
System.out.println();
count = 1;
}
}
}
public static List<String> getWeekends(int year){
List<String> list = new ArrayList<String>();
final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance(Locale.CHINA);
cal.set(year, 0, 1);
for(int day = 1; day <= cal.getActualMaximum(Calendar.DAY_OF_YEAR); day++){
cal.set(Calendar.DAY_OF_YEAR, day);
int weekDay = cal.get(Calendar.DAY_OF_WEEK);
if(weekDay == Calendar.SATURDAY || weekDay == Calendar.SUNDAY){
list.add(sdf.format(cal.getTime()));
}
}
return list;
}
}
-----------------------------
20110101 20110102 20110108 20110109 20110115 20110116 20110122
20110123 20110129 20110130 20110205 20110206 20110212 20110213
20110219 20110220 20110226 20110227 20110305 20110306 20110312
20110313 20110319 20110320 20110326 20110327 20110402 20110403
20110409 20110410 20110416 20110417 20110423 20110424 20110430
20110501 20110507 20110508 20110514 20110515 20110521 20110522
20110528 20110529 20110604 20110605 20110611 20110612 20110618
20110619 20110625 20110626 20110702 20110703 20110709 20110710
20110716 20110717 20110723 20110724 20110730 20110731 20110806
20110807 20110813 20110814 20110820 20110821 20110827 20110828
20110903 20110904 20110910 20110911 20110917 20110918 20110924
20110925 20111001 20111002 20111008 20111009 20111015 20111016
20111022 20111023 20111029 20111030 20111105 20111106 20111112
20111113 20111119 20111120 20111126 20111127 20111203 20111204
20111210 20111211 20111217 20111218 20111224 20111225 20111231