我尝试编写一个代码,显示过去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天,它显示如下:
周四 星期三 周一 周五 周一 星期三 星期四 星期四 星期三 周一 周五
我在哪里弄错了?
答案 0 :(得分:0)
试试这个。
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.
}
答案 1 :(得分:0)
第一个错误是使用与Java捆绑在一起的java.util.Date和.Calendar类。众所周知,它们很麻烦。避免它们。
使用体面的日期时间库。在Java中,这意味着:
两者各有利弊。
两者都提供LocalDate
类,您只需要表示日期,而不需要任何时间部分。
问题的代码将日期时间值与其字符串表示混合在一起。最好使用日期时间值完成工作。然后,分别创建String表示以便呈现给用户。这个想法是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.
}
}
之后,我们以本地化的String格式显示这些值。
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]