【问题标题】:processing last 10 working(business) days java处理最后 10 个工作日(工作日)java
【发布时间】:2014-08-09 21:30:01
【问题描述】:

我尝试编写一个代码来显示最近 10 个日期的星期几。

这是部分代码:

Calendar cal = Calendar.getInstance();    
for(int i=0; i<=9;i++) {
    cal.add(Calendar.DATE, -i);
    Date tday=cal.getTime();
    SimpleDateFormat dy = new SimpleDateFormat("EEE");
    String d9 = dy.format(tday);
    System.out.println(d9);
 }

不是按顺序显示所有过去 10 天,而是这样显示:

周四 星期三 星期一 周五 星期一 星期三 星期四 星期四 星期三 星期一 周五

我在哪里犯错了?

【问题讨论】:

  • 你想写代码吗?是什么阻止你尝试?我的建议:首先学习 Java,至少要能够编写它
  • 好的。一切顺利。请继续。如果您卡在中间,请告诉我们。
  • 公共假期呢???
  • 您可以从 SO 获得所需的所有帮助,但您需要先自己付出一点努力。你试过什么?你遇到了什么问题?
  • 对不起,我没有详细说明。我将尝试再次发布以寻求帮助。感谢您的支持。

标签: java datetime calendar


【解决方案1】:

避免 j.u.Date

第一个错误是使用 Java 捆绑的 java.util.Date 和 .Calendar 类。他们是出了名的麻烦。避开他们。

使用合适的日期时间库。在 Java 中,这意味着:

  • Joda-Time
  • Java 8 中的 java.time 包(受 Joda-Time 启发)

两者各有利弊。

两者都提供LocalDate 类,您需要用它来表示一个没有任何时间部分的纯日期。

日期时间与文本

问题的代码将日期时间值与其字符串表示形式混合在一起。最好使用日期时间值完成您的工作。之后,分别创建字符串表示以呈现给用户。这个想法是separation of concerns,让你的代码更清晰,更容易测试/调试。

乔达时间

Java-Time 中的示例代码。

您必须指定一周中的哪几天是工作日。

注意时区的使用。当前日期取决于您在地球上的位置(时区)。巴黎的新一天比蒙特利尔更早。如果省略时区,则应用 JVM 的默认值。最好指定,即使通过调用 getDefault(),也比依赖隐式默认值。

首先,我们收集所需日期时间值的集合。

int requiredCountOfDays = 10; // The Question demands 10 previous working days.
List<LocalDate> days = new ArrayList<LocalDate>( requiredCountOfDays );  // Collect desired LocalDate objects.
DateTimeZone timeZone = DateTimeZone.forID( "Europe/Paris" ); // Specify time zone by which to get current date.
LocalDate today = LocalDate.now( timeZone ); // Get the current date at this moment in specified time zone.
LocalDate localDate = today; // Define var to decrement for previous days.
while ( days.size() < requiredCountOfDays ) { // Loop until we fill the list (10 elements).
    localDate = localDate.minusDays( 1 ); // Decrement to get previous day.
    // Hard-code what days are business days vs weekend days.
    boolean isWeekend = ( ( localDate.getDayOfWeek() == DateTimeConstants.SATURDAY ) || ( localDate.getDayOfWeek() == DateTimeConstants.SUNDAY ) ); // Hard-coding for weekend because it is easier to type than coding for the more numerous week days.
    if ( !isWeekend ) { // If business day…
        days.add( localDate ); // …collect this day.
    }
}

之后,我们以本地化的字符串格式呈现这些值。

List<String> daysOfWeek = new ArrayList<String>( days.size() ); // Collect the same number of LocalDate objects, rendered as Strings.
DateTimeFormatter formatter = DateTimeFormat.forPattern( "EEE" ); // Generate name of day-of-week, abbreviated.
for ( LocalDate day : days ) {
   String dayOfWeek = formatter.print( day ); // Generate String representation.
   daysOfWeek.add( dayOfWeek ); // Collect the string.
}

转储到控制台...

System.out.println( "days: " + days );
System.out.println( "daysOfWeek: " + daysOfWeek );   

运行时……

days: [2014-06-18, 2014-06-17, 2014-06-16, 2014-06-13, 2014-06-12, 2014-06-11, 2014-06-10, 2014-06-09, 2014-06-06, 2014-06-05]
daysOfWeek: [Wed, Tue, Mon, Fri, Thu, Wed, Tue, Mon, Fri, Thu]

【讨论】:

    【解决方案2】:

    试试这个。

            boolean work = true;
            int day = 0; // 0 = today, 1 = yesterday etc...
            int subDay = 0; // subtract day 
    
    
            while (work){   
            Calendar cal = Calendar.getInstance(); // get current time
            cal.add(Calendar.DAY_OF_WEEK, subDay); // subtract day
    
            // working days are Mon, Tue, Wed, Thu, Fri. If we get saturdays or sundays, we want to skip that days,
            // so we use if declaration
    
    
            if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                subDay--;
                continue;
            }
    
    
    
               Date tday=cal.getTime(); 
               SimpleDateFormat dy = new SimpleDateFormat("EEE");
               String d9 = dy.format(tday);
               System.out.println("Day: " + day + " - " + d9);  
    
               day ++; 
               subDay--;
    
               if (day >= 10){work=false;} // here we declara how much day we want to go back, and we break loop.
    
            }
    

    【讨论】:

    • 我添加了一些cmets,希望现在一切都清楚了。
    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 2017-01-20
    • 2023-02-15
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2013-04-08
    • 2022-06-10
    相关资源
    最近更新 更多