Determine if the date is the day of the week / day off java

I’m trying to compare it on that day of the week, and if it’s a weekend, then follow the action (not yet implemented), but if I set the date today 11/25/2016 or tomorrow, Saturday 11/26/2016, it still prints only " WEEKDAY. " Its not working and I am stuck: /

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");
    }
}
+4
source share
3 answers

In the if statement, you forgot c1.get on Sunday. It should be like this:

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

And why are you sending boolean b and double c? It has never been used.

+7
source
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");
}
-1
source

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
}
Hide result
-2

Source: https://habr.com/ru/post/1661937/


All Articles