-4

I want to check if the date is weekend (lets say 28 august 2016), but for some arkane reason it returns the wrong day The code is as follows

SimpleDateFormat format = new SimpleDateFormat("yyy,mm,dd");
depDate = format.parse(departure_date);
Calendar calDeparture = Calendar.getInstance();
calDeparture.setTime(depDate);
if (calDeparture.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY && calArrival.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        Log.d("true", "WEEKEND");
    }else {
        Log.d("FALSE", "NOT WEEKEND");
    }

for some strange reason when i do Log.d("DAY OF WEEk:", Integer.toString(calDeparture.get(Calendar.DAY_OF_WEEK))); on the 28 of august it returns 4

Jens
  • 67,715
  • 15
  • 98
  • 113
denis
  • 1
  • 4
  • 1
    "mm" means minutes, not months... if you look at the result of the parse, you'll see what's going on. It's also very odd to use commas in a date format. – Jon Skeet Aug 23 '16 at 08:31
  • i get the same result. nothing changes. the 28 of august prints out 4th day of week – denis Aug 23 '16 at 08:45
  • 2
    In that case, you need to provide a [mcve] showing the issue. – Jon Skeet Aug 23 '16 at 08:46
  • @JonSkeet I managed to get it work. the problem was this line of code: departureDateTrue = Integer.toString(year)+","+Integer.toString(month)+","+Integer.toString(day); i changed it departureDateTrue = Integer.toString(year)+","+Integer.toString(month+1)+","+Integer.toString(day); – denis Aug 23 '16 at 09:28
  • When the problem is in code that isn't in the question, it's really hard for us to help you. Please learn from this experience: a question with a [mcve] is much better than an incomplete one like this. Also note that you'd be better off *not* formatting the date like that to start with, but instead populating a `Calendar` (or better, a Java 8 time value) and formatting that. As it is, your value won't match the specified format when the day number or month number is less than 10... – Jon Skeet Aug 23 '16 at 09:42
  • When this was your problem, then this is a suitable dupe: [Why are months off by one with Java SimpleDateFormat?](http://stackoverflow.com/q/2992381) – Tom Aug 23 '16 at 09:42

1 Answers1

1

Your format is wrong. Month must be uppercase MM

SimpleDateFormat format = new SimpleDateFormat("yyyy,MM,dd");

lowercase mm is Minute.

    SimpleDateFormat format = new SimpleDateFormat("yyy,MM,dd");
    Date depDate = format.parse("2016,8,28");
    Calendar calDeparture = Calendar.getInstance();
    System.out.println(depDate);
    calDeparture.setTime(depDate);
    System.out.println(calDeparture.get(Calendar.DAY_OF_WEEK));

Output is:

Sun Aug 28 00:00:00 CEST 2016
1
Jens
  • 67,715
  • 15
  • 98
  • 113
  • i get the same result. nothing changes. the 28 of august prints out 4th day of week – denis Aug 23 '16 at 08:43
  • @denis for me it works fine and returns 1. See my excample. – Jens Aug 23 '16 at 08:50
  • I managed to get it work. the problem was this line of code: departureDateTrue = Integer.toString(year)+","+Integer.toString(month)+","+Integer.toString(day); i changed it departureDateTrue = Integer.toString(year)+","+Integer.toString(month+1)+","+Integer.toString(day); – denis Aug 23 '16 at 09:26
  • @denis that is code you not Show in your question – Jens Aug 23 '16 at 09:27
  • I know, but i thought it wasn't important. Actually i have a question as .get(Calendar.MONTH); returns 7 (for august) why i needed to change it to 8 for the code to work – denis Aug 23 '16 at 09:33
  • @denis it is Zero based – Jens Aug 23 '16 at 09:43