9

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 :/

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");
    }
}
Aidan Moore
  • 129
  • 1
  • 2
  • 6
  • 3
    you forgot the `c1.get` in your if: `c1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY` not `Calendar.DAY_OF_WEEK == Calendar.SUNDAY` – OH GOD SPIDERS Nov 25 '16 at 12:20
  • 1
    Some countries have different weekends. Maybe you find my method to [implement a localized weekend](http://time4j.net/javadoc-en/net/time4j/PlainDate.html#isWeekend-java.util.Locale-) interesting. – Meno Hochschild Nov 25 '16 at 12:56
  • Please search Stack Overflow before posting. – Basil Bourque Nov 26 '16 at 06:18

3 Answers3

21

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

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.

Alex Karlsson
  • 296
  • 1
  • 11
2
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");
}
Tchopane
  • 175
  • 8
-5

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

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
}
Shekhar Patel
  • 641
  • 10
  • 20