仅打印工作日

时间:2015-12-16 22:33:14

标签: java arrays calendar simpledateformat

在过去的4个小时里,我一直坚持这个问题尝试不同的策略,这些策略在这里检查了无数的帖子和谷歌。基本上我要做的就是打印一周内的商业工作日。我每次周末的日子都会尝试增加一天的日期,但当然这会让日期的顺序变得混乱。例如,而不是:

 Iteration: 0, 1, 2, 3, 4, 5, 6, 7, 8....

 Day: (0)Friday, (1)Saturday, (2)Sunday, (3)Monday, (4)Tuesday, (5)Wednesday, (6)Thursday, (7)Friday, (8)Saturday....etc

我要求:

   Day: (0)Friday, (1)Monday, (2)Tuesday (3)Wednesday, (4)Thursday, (5)Friday, (6)Monday, (7)Tuesday, (8)Wednesday....etc

以下是代码:

public static void date(int day) {
    now = Calendar.getInstance();
    //SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");//version 1
    SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy"); //version 2

    String[] days = new String[maxDayCount];      //limits the number of days to print out(ordinarily h)
    now.add(Calendar.DAY_OF_YEAR, day + 1);     //Increments from test count + 1 due to count starting at 0 (would be today). Increments the date to get from today.

    int DayOfWeek = now.get(Calendar.DAY_OF_WEEK);
    boolean WorkingDayCheck = ((DayOfWeek >= Calendar.MONDAY) && (DayOfWeek <= Calendar.FRIDAY));

    if(WorkingDayCheck) {
        days[day] = format.format(now.getTime());
    } else {
      //
    }
    now.add(Calendar.DAY_OF_MONTH, 1);
    System.out.println("-----------Day " + day + ": " + String.valueOf(days[day]));    //v2: print out each line
}//END OF METHOD: date

我的想法是,我在其他地方进行了迭代,将日期号传递给此方法,以便使用该数字然后打印出日期。我现在回到原来的状态,然后尝试了大量的事情,目前正在检查我正在使用WorkingDayCheck布尔值,打印输出当然是在周末时间到来时返回null。

任何想法都在破裂? 谢谢你的时间。

3 个答案:

答案 0 :(得分:0)

我认为这可以解决您的问题。

首先,为什么每次输入方法时都定义数组days,然后只填充其中的一个值?

您的String[] days = new String[maxDayCount];应该是成员变量而不是局部变量。所以在这个方法之外初始化这个数组。

同时初始化另一个代表数组中最后一个空位置的int。 (默认为0

public class Foo {
  int maxDayCount = 365;
  String[] days = new String[maxDayCount];
  int finalIndex = 0;

  public static void date(int day) {
    now = Calendar.getInstance();
    SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy");

    String[] days = new String[maxDayCount];      //limits the number of days to print out(ordinarily h)
    now.add(Calendar.DAY_OF_YEAR, day + 1);     //Increments from test count + 1 due to count starting at 0 (would be today). Increments the date to get from today.

    int DayOfWeek = now.get(Calendar.DAY_OF_WEEK);
    boolean WorkingDayCheck = ((DayOfWeek >= Calendar.MONDAY) && (DayOfWeek <= Calendar.FRIDAY));

    if(WorkingDayCheck) {
        days[finalIndex++] = format.format(now.getTime());
        System.out.print("(" + (finalIndex - 1) + ")" + String.valueOf(days[finalIndex - 1]))
    } else {
      //
    }
  }
  public static void main(String[]args) {
    int day = 0;
    while(finalIndex != maxDaysCount) {
      date(day++);
    }
  }
}

现在,您的days数组将仅在其各自的索引中填充工作日。例如,days[0]中的Fridaydays[1]Saturday将为finalIndex,依此类推,因为您的指针day独立于targetLine.open(format, BUFFER_SIZE); sourceLine.open(format, BUFFER_SIZE); 的值1}}从迭代中输入到您的方法。

答案 1 :(得分:0)

如果我正确地解决了您的问题,您可能会喜欢跟随,

Working Day #1 = 0 Week + 1 Day = 1st Day
..
Working Day #5 = 0 Week + 5 Days = 5th Day
Working Day #6 = 1 Week + 1 Day = 8th Day
..
Working Day #10 = 1 Week + 5 Days = 12th Day

然后打印一年中第1,第2,第3 日历日。

如果是这种情况,您可以尝试从工作日查找日历日,



    int div = day/5;
    int rem = day%5;
    int calendarDay = 0;
    if(day>5){
        if(rem == 0)
            calendarDay = (div-1)*7 + 5;
        else
            calendarDay = div*7 + rem;
    }else
        calendarDay = day;

然后找到该日期的日历日,如



    Calendar calendar = new GregorianCalendar();
    calendar.set(Calendar.DAY_OF_YEAR, day);
    StringBuilder sb = new StringBuilder();
    Formatter formatter = new Formatter(sb, Locale.ENGLISH);
    formatter.format("%tD", calendar);
    System.out.println(sb);

代码不是解决方案,但我想它指向你想去的地方。

答案 2 :(得分:0)

玩过代码后,我已经实现了以后的目标:

    public static String getDate4(int day) {
       now = Calendar.getInstance();
       //SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");//version 1
       SimpleDateFormat format = new SimpleDateFormat("EEEE d MMMM yyyy"); //version 2

       now.add(Calendar.DAY_OF_YEAR, day + 1);     //Increments from test count + 1 due to count starting at 0 (would be today). Increments the date to get from today.

       int DayOfWeek = now.get(Calendar.DAY_OF_WEEK);
       boolean WorkingDayCheck = ((DayOfWeek >= Calendar.MONDAY) && (DayOfWeek <= Calendar.FRIDAY));

       if(WorkingDayCheck) {
            days[finalIndex++] = format.format(now.getTime());
        return "\n(" + (finalIndex - 1) + ") " + String.valueOf(days[finalIndex - 1]);
        } else {
            now.add(Calendar.DATE, 2);  //advance dates by two
            dayCount = dayCount + 2;    //Increase the day count by 2 to ensure that later dates are also incremented to avoid duplication i.e. Monday(Sat), Tuesday(Sun), Monday, Tuesday, Wed...
            days[finalIndex++] = format.format(now.getTime());
          return "\n(" + (finalIndex - 1) + ") " + String.valueOf(days[finalIndex - 1]);
       }//END OF if else
    }//END OF getDate4 method

我不得不添加和使用外部dayCount(类似于@Mohammed Osama使用的那一天),与主管方法迭代中使用的计数分开,我之前用它来提供这种方法然后当周末出现时我将日期增加2,这是我之前尝试过的事情,然而当周末结束时问题出现了,并且它又回到了非周末日期,结果是:

Friday
Monday (previously Saturday)
Tuesday (previously Sunday)
Monday (weekend over, now back onto Monday) <--- Issue here and onwards
Tuesday

这是新的dayCount进入的地方,现在只需将此方法输入此方法:

//Main method
//Iteration code start
getDate4(dayCount++);
//Iteration end

每次遇到一个周末时,将此值增加2会将后来的日期推到适当的位置,因此现在我得到的是我以后的总是:

Friday
Monday (previously Saturday) <-Pushed ahead by 2 dates to Monday
Tuesday (previously Sunday)  <-Pushed ahead by 2 dates to Tuesday
Wednesday (previously Monday) <-Pushed ahead 2 dates to Wednesday due to weekends incrementing the dayCount by 2
Thursday <--- etc. etc.

非常感谢帮助人员,最后可以继续前进:&gt;