this is my code:
import java.util.Calendar;
import java.util.Locale;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Calendar a = Calendar.getInstance();
a.set(Calendar.MONTH, 7);
a.set(Calendar.YEAR, 2011);
a.set(Calendar.DAY_OF_MONTH, 16);
System.out.print(test.getDate(a));
}
// What-date calculation
public static String getDate(Calendar date) {
//date = Calendar.getInstance();
return date.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG,
Locale.getDefault());
}
}
when I run this code, it prints “Tuesday”. However, I run this code on July 16, 2011 and uncomment
date = Calendar.getInstance();
so that date would be July 16, 2011 and it prints exactly “Saturday”.
I wonder if the Calendar object a is passed correctly because a is also set July 16, 2011.
Is there any solution for me to pass Calendar object and print the date correctly?
Thank in advance
Don’t use “magic” numbers when setting Calendar’s month since they are 0 based (not 1 based as you have assumed). Instead use the Calendar constants, such as Calendar.JULY.