java获取每年所有月份的的周六日的日期

  • Post author:
  • Post category:java


/**

* 计算周六日

* @param args

* @throws ParseException

*/

public static void main(String[] args) throws ParseException {


int year = 2018;

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

SimpleDateFormat simdf = new SimpleDateFormat(“yyyy-MM-dd”);

Calendar calendar = new GregorianCalendar(year, 0, 1);

int i = 1;

while (calendar.get(Calendar.YEAR) < year + 1) {


calendar.set(Calendar.WEEK_OF_YEAR, i++);

calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

if (calendar.get(Calendar.YEAR) == year) {


System.out.println(“周日:”+simdf.format(calendar.getTime()));

dateList.add(simdf.format(calendar.getTime()));

}

calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);

if (calendar.get(Calendar.YEAR) == year) {


System.out.println(“周六:”+simdf.format(calendar.getTime()));

dateList.add(simdf.format(calendar.getTime()));

}

}

System.out.println(dateList.size());

}



版权声明:本文为cymlancy原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。