0

Edit: Thanks- I understand it a lot more now. Its very confusing when you first start! Thanks for the help. I am going to keep the question up as is ( in peril of more downvotes) as it might help others. There are some other libraries that everyone seems to recommend for date time


I am struggling with the Java calendar function- its seems to be returning wrong data the bottom figure should be a thursday according to calendar, but is returning as a saturday!

Calendar cal = new GregorianCalendar();
       cal.set(2012,2,23); // 0 = January

      String weekdays[]={"sunday","monday", "tuesday", "wednesday","thursday","friday","saturday",};

        Integer Weekdaycurrent1=cal.get(Calendar.DAY_OF_WEEK);
        System.out.println("today is a "+weekdays[Weekdaycurrent1]); //Prints today is Saturday, when it should be a thursday
UKDataGeek
  • 6,338
  • 9
  • 46
  • 63
  • 7
    It's a bad idea to assume that a class that's been part of the JDK since 1.0 is "wrong". Someone would have found such a trivial bug years ago. You're the one that's wrong. Assume that you're the problem and you'll make faster progress. – duffymo May 14 '12 at 00:50
  • 1
    Your post title sounds as if you've found a bug in Java, which of course isn't true. Consider editing your thread and re-titling it, "Which of my assumptions regarding the Calendar class are wrong", or something similar. Do this and I'll bet the down-votes will go away. – Hovercraft Full Of Eels May 14 '12 at 01:12
  • Thanks- I understand it a lot more now. Its very confusing when you first start! Thanks for the help. I am going to keep the question up ( in peril of more downvotes) as it might help others – UKDataGeek Jul 01 '12 at 10:48

2 Answers2

4

For starters, DAY_OF_WEEK is 1 based:

public final static int SUNDAY = 1;

Secondly, 2012-03-23 (yes, Mar not Feb) as set by cal.set(2012, 2, 23) was a Friday

Your code is behaving correctly.

Edited: For those too lame to read the question properly, calling cal.set(2012,2,23) sets the date to 2012-03-23, because the month parameter is zero-based (ie Jan = 0, Feb = 1, Mar = 2, etc)

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • @Jack Dude, `cal.set(2012, 2, 23)` sets it to *March*, **not** *February*, because the month parameter is zero-based (Jan = 0) – Bohemian May 14 '12 at 01:23
  • There has been some overlapping between human month convention and Java month convention here ;) I actually know that it is zero based but when I've read your answer I supposed your `2012-03-23` was zero based too just because I had been lazy, sorry about that! – Jack May 14 '12 at 02:45
  • Thanks for the feedback. It just seems weird the implementation of calendar on java. Why not just make everything zero based or make everything 1 based. Doesn't make sense really. – UKDataGeek May 14 '12 at 07:32
2

Wrong assumption on your part. Read the javadocs:

http://docs.oracle.com/javase/1.4.2/docs/api/constant-values.html#java.util

public static final int SUNDAY          1
public static final int MONDAY          2
public static final int TUESDAY         3
public static final int WEDNESDAY       4
public static final int THURSDAY        5
public static final int FRIDAY          6
public static final int SATURDAY        7
duffymo
  • 305,152
  • 44
  • 369
  • 561