确定日期是否为工作日/周末 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40804890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:55:43  来源:igfitidea点击:

Determine if date is weekday/weekend java

javadatecalendar

提问by Aidan Moore

I'm trying to make it compare what day of the week it is and if it is a weekend then perform an action(not implemented yet) but if I put in today's date 25/11/2016 or tomorrows which is a Saturday 26/11/2016, it still only prints "WEEKDAY". Its not working and I'm stuck :/

我试图让它比较一周中的哪一天,如果它是周末然后执行一个操作(尚未实施)但是如果我输入今天的日期 25/11/2016 或明天是星期六 26/ 11/2016,它仍然只打印“WEEKDAY”。它不起作用,我被卡住了:/

public static void calcDatePrice(String a, boolean b, double c){
    System.out.println("CALC PRICE METHOD: " + a);
    Double price;
    Date d1 = new Date(a);

    Calendar c1 = Calendar.getInstance();
    c1.setTime(d1);
    System.out.println(c1.get(Calendar.DAY_OF_WEEK));

    if ((c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY) 
            || (Calendar.DAY_OF_WEEK == Calendar.SUNDAY)) {  //or sunday   
    System.out.println("WEEKEND PRICE");
    }else {
    System.out.println("WEEKDAY");
    }
}

回答by Tchopane

if ((c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY)  || (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)) { 
    System.out.println("WEEKEND PRICE");
} else {
    System.out.println("WEEKDAY");
}

回答by Alex Karlsson

In your if statement you forgot the c1.get on sunday. Should be like this:

在您的 if 语句中,您忘记了星期天的 c1.get。应该是这样的:

if (c1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || 
    c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)

Also why do you send in the boolean b and double c? It's never used.

另外你为什么要发送布尔值 b 和双 c?它从来没有使用过。

回答by Shekhar Patel

you can use calender.DayofWeek method to find which day it is

您可以使用 calender.DayofWeek 方法来查找今天是哪一天

Calendar cl = Calendar.getInstance();
c1.setTime(Enter Yor Date);
int day = c.get(Calendar.DAY_OF_WEEK);
if(check day value is 5 or 6)
  {
   // Weekend Coding
  }
Else
{
  // Weekday Coding
}