2013-02-11 29 views
0

我该如何去排除我的打印输出中的任何一天,即星期五和本月的第13天。我正在尝试写下如下内容:if(dayofweek!= 5 & & dayofmonth!= 13),然后打印。我怎么能把它实现到下面的代码?不包括打印循环中的日期

public class LoopDate { 

public static void main(String[] args) { 

    //Denotes that Tuesday is the first day of 2013 
    int startingDayOfWeek = 2; 
    int year = 2013; 
    int numDays = 0; 
    for (int month = 1; month <= 12; month++) { 
     switch (month) { 
     case 1: 
     case 3: 
     case 5: 
     case 7: 
     case 8: 
     case 10: 
     case 12: 
      numDays = 31; 
      break; 
     case 4: 
     case 6: 
     case 9: 
     case 11: 
      numDays = 30; 
      break; 
     case 2: 
      if (((year % 4 == 0) && !(year % 100 == 0)) 
        || (year % 400 == 0)) 
       numDays = 29; 
      else 
       numDays = 28; 
      break; 
     default: 
      System.out.println("Invalid month."); 
      break; 
     } 
     for (int start = 1; start <= numDays; start++) 
      System.out.println(month + "/" + start); 


     } 
    } 
} 
+0

不是那样的,一...查看我的答案了(更合理的)可能性。 – hd1 2013-02-11 05:05:01

+0

组成我的型号 – hd1 2013-02-11 05:06:20

回答

1

尝试

GregorianCalendar c = new GregorianCalendar(2013, 0, 1); 
    while (c.get(Calendar.YEAR) == 2013) { 
     if (!(c.get(Calendar.DAY_OF_MONTH) == 13 && c.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY)) { 
      System.out.println(c.getTime()); 
     } 
     c.add(Calendar.DAY_OF_YEAR, 1); 
    } 
+0

(+1)干净的工作方式! – 2013-02-11 07:02:09

0

使用joda time,你只是做:

DateTime today = new DateTime(); 
if (today.dayOfWeek().getAsText().equals("Friday")) { continue; } 
if (today.dayOfMonth().get() == 13) { continue; } 
// print here 
+0

如果您在用户档案中检查OP的问题,看起来他/她处于学习阶段,因此在这种情况下添加外部库无助于您。 – 2013-02-11 05:18:01